Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer-to-member function of derived class

Reply
Thread Tools

Pointer-to-member function of derived class

 
 
Rich Talbot-Watkins
Guest
Posts: n/a
 
      03-04-2004
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


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-04-2004
Rich Talbot-Watkins wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived class function masks base class function Lionel B C++ 8 02-05-2009 10:16 PM
call base class function or derived class function George2 C++ 0 03-17-2008 02:03 AM
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57