Skip to main content
GameDev.net gamedev.net
🔒 Locked

Function pointers between classes

Started by Mantear Oct 13, 2006 at 6:36 PM 1 replies 500+ views
Original Post
Mantear
Mantear
Greetings! I can never seem to fully wrap my head around function pointers, so every time I come back to them, I need help. Using VS 2005 Express, here's my setup.
class Class_B;
class Class_C;

class Class_A
{
   Class_A() {}
   ~Class_A() {}

   void Class_A_MmbrFnc();

   Class_B m_objB;
};

class Class_B
{
   Class_B() {}
   ~Class_B() {}

   void Class_B_MmbrFnc();

   Class_C m_objC;
};

class Class_C
{
   Class_C() {}
   ~Class_C() {}

   void Class_C_MmbrFnc();
};
Class_C is the class that will have the function pointer (not shown). Ideally, for my situation, I would like the function pointer in Class_C to be able to point to either Class_B_MmbrFnc() or Class_A_MmbrFnc(). (All of the functions I want to point to are going to have the signature of void Function(void), if that makes a difference.) If that's not possible, I would like it to point to Class_A_MmbrFunc(). How can I achieve this? Specifically, what is the syntax for: 1) Declaring the member variable in Class_C to hold the function pointer. 2) Passing the function pointer from Class_B to Class_C. 3) Calling the function using the function pointer. I'm currently looking at the tutorials on-line (which there are several what appear to be good ones), but I'm still having trouble getting it right. Thanks!
dave
dave
Have a look here. This is what i refer to when i need to revitalise my function pointer knowledge.

Hope it helps,

Dave
yckx
yckx
Quote:
Original post by MantearClass_C is the class that will have the function pointer (not shown). Ideally, for my situation, I would like the function pointer in Class_C to be able to point to either Class_B_MmbrFnc() or Class_A_MmbrFnc(). (All of the functions I want to point to are going to have the signature of void Function(void), if that makes a difference.) If that's not possible, I would like it to point to Class_A_MmbrFunc(). How can I achieve this?

The syntax would be
void (Class_A::* fn_ptr) ()
I believe that if you want a function pointer able to point to a method of either class, you'll have to derive them from a base class and declare the method in the base class. Of course, I'll be the first to admit that I might not be entirely correct here, but I think I am :D

Topic Locked

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

Sign in to reply to this topic.