Skip to main content
gamedev.net
🔒 Locked

method pointer

Started by ironfroggy 3 replies 350+ views
Original Post
ironfroggy
ironfroggy
in C, function pointers were a good asset. But, in the OOP world of C++, are there method pointers? I don''t mean anything through some abstraction, I mean an actual pointer to the method for a particular instance of a class. // God.c void main() { WORLD Earth; LIFE People = Earth.CreateLife(HUMAN); GiveHope(&People); delete Earth; EvilCackle(); }
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
mutex
mutex
You can only have method pointers to static members, because to call a method for a particular instance of a class, you need a pointer to that class instance also. One way is to do the following:

class MyClass
{
void static_method(MyClass* p) { p->method(); }
void method() { }
};

You can store static_method as a function pointer and call it some time in the future as long as you have stored the class pointer too.

ironfroggy
ironfroggy
Could it be possible for a compiler to allow for method pointers? non-static, that is.

Perhaps, if you tried to use a method pointer to a non-static method, the compiler could create a very small function to call the method of the correct object?

Maybe the GCC people could look into this?
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Grugnorr
Grugnorr
You can have pointers to methods...

If this is the way to declare a pointer to a function:

ReturningType (*PointerToFunctionName)(Parameters)

This is the way to declare a pointer to a method:

ReturningType ClassName:*PointerToMethodName)(Parameters)

.-I know it can be done, I think it is this way... you don´t use many pointer to methods in OOP...

What the hells!
What the hells!
DrPizza
DrPizza
quote:
Original post by ironfroggy
in C, function pointers were a good asset. But, in the OOP world of C++, are there method pointers? I don''t mean anything through some abstraction, I mean an actual pointer to the method for a particular instance of a class.



Yes, there are pointers to member functions.

As with all function pointers, it''s best to use a typedef.

  
typedef return_type (class_type::* function_pointer_type)(parameter_type1, parameter_type2 /* etc. */);

So, for instance, a class MyClass with a function taking a long and a double and returning bool:
  
typedef bool (MyClass::* fp_type)(long, double);

To make the function pointer point at something, you don''t point it at the class''s function definition (*not* a particular instantiation of a class):
  
fp_type theFunctionPointer = &MyClass::theFunction;

which makes theFunctionPointer point at the "theFunction" method of MyClass.

To use it, you need a MyClass to work on, and then use a rather ugly bit of syntax (notice the .* operator):
  
MyClass theClass;
theClass.*theFunctionPointer(-20, 0.1);

There is similarly a ->* operator, for if you had a pointer to the class.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.