The best way to have a pointer to several methods - critique requested
Posted
by
user827992
on Programmers
See other posts from Programmers
or by user827992
Published on 2012-06-23T20:31:42Z
Indexed on
2012/06/23
21:24 UTC
Read the original article
Hit count: 298
I'm starting with a short introduction of what i know from the C language:
- a pointer is a type that stores an adress or a NULL
- the
*
operator reads the left value of the variable on its right and use this value as address and reads the value of the variable at that address - the
&
operator generate a pointer to the variable on its right
so i was thinking that in C++ the pointers can work this way too, but i was wrong, to generate a pointer to a static method i have to do this:
#include <iostream>
class Foo{
public:
static void dummy(void){ std::cout << "I'm dummy" << std::endl; };
};
int main(){
void (*p)();
p = Foo::dummy; // step 1
p();
p = &(Foo::dummy); // step 2
p();
p = Foo; // step 3
p->dummy();
return(0);
}
now i have several questions:
- why step 1 works
- why step 2 works too, looks like a "pointer to pointer" for p to me, very different from step 1
- why step 3 is the only one that doesn't work and is the only one that makes some sort of sense to me, honestly
- how can i write an array of pointers or a pointer to pointers structure to store methods ( static or non-static from real objects )
- what is the best syntax and coding style for generating a pointer to a method?
© Programmers or respective owner