01-16-2004, 07:54 AM | #1 (permalink) |
Insane
Location: Seattle
|
[C++] Challenge
So recently I watched from a distance as two qualified coworkers tried for maybe 3 hours to figure out this C++ quandry.
In a method, create a table of pointers to non static member functions of class CFoo. Take an instance of CFoo and call out on the member functions from the table.
__________________
"It's a long story," says I, and let him up. |
01-16-2004, 08:06 AM | #2 (permalink) |
WARNING: FLAMMABLE
Location: Ask Acetylene
|
Uhmm... that sounds like it is semantically nonsensical.
You should not be able to create pointers to non-static member functions of a class. ASSUMING that the compiler let you do this (it is possible that It might let you create the function pointer anyway even though they are non-static) you can't call them without passing in the this pointer. Non-static member (Static member functions don't need the this pointer because the location of the unique static instance is fixed in memory) functions always have a this pointer that needs to be passed in by the compiler implicitly, when you call the functions from a table your not passing in the *this pointer to the CFoo instance. If the compiler allows you to explicitly pass in CFoo as the first argument to the functions as you call from that table then it might make sense semantically. Semantics of the language isn't everything though. The compiler may expressly forbid you from creating those function pointers cuz it's just sorta fubar. I don't do C++ anymore, anyway why the heck do you want to do this? There has to be another more elegant way, you could just pass a pointer to the instance of CFoo itself to the right spot.
__________________
"It better be funny" |
01-16-2004, 04:04 PM | #3 (permalink) |
Junkie
Location: San Francisco
|
You mean like this?
Code:
#include <iostream> using namespace std; class CFoo { public: void func1() { cout << "func1 for CFoo at " << this << endl; } void func2() { cout << "func2 for CFoo at " << this << endl; } }; typedef void (CFoo::*FooMethod)(); int main() { FooMethod methods[2]; methods[0] = &CFoo::func1; methods[1] = &CFoo::func2; CFoo foo; (foo.*methods[0])(); (foo.*methods[1])(); return 0; } Last edited by n0nsensical; 01-16-2004 at 04:08 PM.. |
01-18-2004, 03:52 PM | #4 (permalink) |
Insane
Location: Seattle
|
Yes, just like that.
For those that say this is not possible, check your Stroustroup. As far as this being a poor idea in an object oriented language - I'd agree in most cases there are more elegant solutions.
__________________
"It's a long story," says I, and let him up. |
01-21-2004, 02:49 PM | #6 (permalink) |
Location: Waterloo, Ontario
|
I hope I'm not being pedantic but I would have written the last part out like this:
Code:
// this is simple but, unfortunately, this will erroneously compile with a pointer parameter... #define NUM_OF(x) (sizeof(x)/sizeof(x[0])) // this will generate a run-time compile error if the parameter is _not_ an array! template <typename T, size_t size> size_t num_of(T(&)[size]) { return size; } int main() { FooMethod methods[2]; methods[0] = CFoo::func1; // like arrays, functions are already pointers, so... methods[1] = CFoo::func2; // ...you don't need to dereference them... CFoo foo; for(int i = 0; i < num_of(methods); ++i) { (foo.*methods[0])(); (foo.*methods[1])(); } return 0; } The for() loop was just for style, as is the num_of() template function. Although far more complicated than the macro, it's safer 'cause the marcro will erroneously compile if you use a pointer instead of an array. The function only makes sense if you're passing in an array of objects... The double spacing is annoying. I would have used the quote block except it removes all leading white space. By the way, n0nsensical, how on Earth did you enter tabs into the text box? Thanks! |
01-21-2004, 03:07 PM | #7 (permalink) | |
Junkie
Location: San Francisco
|
Quote:
|
|
01-21-2004, 03:23 PM | #8 (permalink) | |
Location: Waterloo, Ontario
|
Quote:
Code:
const int foo; int const bar; |
|
Tags |
challenge |
Thread Tools | |
|
|