C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer-to-member function of derived class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rich Talbot-Watkins
Guest





PostPosted: Thu Mar 04, 2004 4:41 pm    Post subject: Pointer-to-member function of derived class Reply with quote



Hi all,

Thought I'd call on the experts to see if there's a solution to my problem -
it's best explained by way of an example. Consider the following code
extract:

//////////////////////////////////////////////////////////////

class CBaseClass;
struct BEHAVIOUR_TABLE
{
bool (CBaseClass::*m_pmfnHandler)();
int m_iWhatever;
};

class CBaseClass
{
public:
bool StandardWait();
};

class CDerivedClass : public CBaseClass
{
public:
static BEHAVIOUR_TABLE m_gastTable[];

bool SpecialisedPatrol();
bool SpecialisedAttack();
};

BEHAVIOUR_TABLE CDerivedClass::m_gastTable[] =
{
{ &StandardWait, 0 },
{ &SpecialisedPatrol, 1 },
{ &SpecialisedAttack, 2 }
};

// Dummy implementations of behaviour methods
bool CBaseClass::StandardWait() { return true; }
bool CDerivedClass::SpecialisedPatrol() { return true; }
bool CDerivedClass::SpecialisedAttack() { return true; }

//////////////////////////////////////////////////////////////

Effectively, I want to be able to declare a static array member for classes
derived from CBaseClass, containing pointers-to-member-functions for any
method of the form 'bool Method()' available to that derived class. This of
course could include superclass methods.

Compiling this produces the error:

a value of type "bool (CDerivedClass::*)()" cannot be used to initialize an
entity of type "bool (CBaseClass::*)()"
{ &SpecialisedPatrol, 1 }
^

Fair enough - but my question is: IS there a way of achieving this, given
the relationship between the two classes?

TIA,
Rich


Back to top
Andrey Tarasevich
Guest





PostPosted: Thu Mar 04, 2004 5:05 pm    Post subject: Re: Pointer-to-member function of derived class Reply with quote



Rich Talbot-Watkins wrote:
Quote:
Compiling this produces the error:

a value of type "bool (CDerivedClass::*)()" cannot be used to initialize an
entity of type "bool (CBaseClass::*)()"
{ &SpecialisedPatrol, 1 }
^

Fair enough - but my question is: IS there a way of achieving this, given
the relationship between the two classes?
...

You have to use 'static_cast' explicitly in this case

BEHAVIOUR_TABLE CDerivedClass::m_gastTable[] =
{
{ &StandardWait, 0 },
{ static_cast<bool (CBaseClass::*)()>(&SpecialisedPatrol), 1 },
{ static_cast<bool (CBaseClass::*)()>(&SpecialisedAttack), 2 }
};

--
Best regards,
Andrey Tarasevich


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.