Archived

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

vbisme

Function pointers and classes

Recommended Posts

vbisme    100
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   
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
IFooBar    906
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
IFooBar    906
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