Archived

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

Function pointers and classes

This topic is 5318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Analyze:
    
class A
{
public:
long AFunc(void(*pfn)(void))
{
//Do something

}
};
long MyFunc();
void main()
{
A MyA;
MyA.AFunc(&MyFunc); //This is ok

}
long MyFunc()
{
//Do something

}
  
As could see, the above is legitimate.  However, 
      
//Assume class A is defined

class B
{
protected:
A m_MyA;

public:
void BFunc()
{
m_MyA.AFunc(BFunc2);  //Illegal

}
long BFunc2()
{
//Do something

}
};
    
I could declare AFunc to take a pointer to a member function of B, but I don't want A to be dependant on B, in case I want to use A in another class. Does anyone know how to go around this? I don't think static member function would work neither. Thanks. [edited by - vbisme on May 27, 2003 4:14:30 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
umm you don''t use the & operator on functions, you simply would do,

m_MyA.AFunc(BFunc);

Share this post


Link to post
Share on other sites
In class B I assume that you meant m_MyA.AFunc(&BFunc2) and not just m_MyA(&BFunc2) right? well anyway...

BFunc is of type

long(B::*pfn)(void)

and it is NOT of type

long(*pfn)(void)

you see BFunc is inside the scope of class B. Thats why it''s B::*pfn instead of just pfn


:::: [ Triple Buffer V2.0 ] ::::

Share this post


Link to post
Share on other sites
quote:
Original post by vbisme
So there is noway I could define AFunc to take a class function pointer unless I specify so?


that is correct. They are different data types. You can however use something like a callback class instead. Im not too sure how it works but...hold on...

*goes to articles section on gd*

...peh, nothing there.

*trots off to flipcode*

!!. I think this is what you might need/want/require:

Clicky




:::: [ Triple Buffer V2.0 ] ::::

Share this post


Link to post
Share on other sites