Pointer-to-member function of derived class

Discussion in 'C++' started by Rich Talbot-Watkins, Mar 4, 2004.

  1. 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
     
    Rich Talbot-Watkins, Mar 4, 2004
    #1
    1. Advertising

  2. 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
     
    Andrey Tarasevich, Mar 4, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,076
    Fraser Ross
    Aug 14, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    824
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    1
    Views:
    411
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    399
    Victor Bazarov
    May 23, 2007
  5. Alex Shen
    Replies:
    5
    Views:
    434
    Alex Shen
    Oct 30, 2010
Loading...

Share This Page