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;
}
Functions are already pointers so, like arrays, you don't need to dereference them when assigning to pointers. In fact, I'm surprised that compilers are hacked to work
despite the dereferencing!
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!