Archived

This topic is now archived and is closed to further replies.

Roof Top Pew Wee

function pointers that accept a class in a class

Recommended Posts

Here's a simplified version of the problem. Say I have class a. Inside is a pointer to a function (this will not be a member function).
  

class a
{public:
  myFunctionType m_functionPointer;
}
  
However, I want the function to accept as it's argument a pointer to class a. So in other words, myFunctionType will be defined as:
  

typedef bool (*myFunctionType ) (a* aPointer); // this is a member of the a class


  
Here I encounter the chicken and egg problem. If I try to put the typedef before the class, it doesn't know what the class a is. And after, the class doesn't know what myFunctionType is. A while back I read an article about how you can cast functions, but I don't remember if there are any specifics, and I can't find the article anymore. If this casting is possible, then I'm thinking of making a basic void (*functionType) (void) function and then casting it later to the myFunctionType. Is this possible, and how would I do this in code? Thanks for any help. --Vic-- Edited by - Roof Top Pew Wee on February 18, 2002 2:37:37 PM

Share this post


Link to post
Share on other sites
simpler than all of this is as follow :

use LPVOID as parameter and then use dynamic_cast to convert at execution into your class. of course this kind of trick is for people who know what they are doing, so be sure to double check type as to not pass some pointer to another class.

hopes this helped

Share this post


Link to post
Share on other sites
If the non-member function takes a "pointer to a" as its parameter, you should be able to forward declare class a:

  class a;
typedef bool (*myFunctionType ) (a* aPointer);
class a
{
public:
myFunctionType m_functionPointer;
};


Edited by - Krunk on February 18, 2002 2:46:26 PM

Share this post


Link to post
Share on other sites