 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rich Talbot-Watkins Guest
|
Posted: Thu Mar 04, 2004 4:41 pm Post subject: Pointer-to-member function of derived class |
|
|
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
|
Posted: Thu Mar 04, 2004 5:05 pm Post subject: Re: Pointer-to-member function of derived class |
|
|
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 |
|
 |
|
|
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
|
|