Pointer to protected member function from derived class

Discussion in 'C++' started by Dmitry, Jun 9, 2008.

  1. Dmitry

    Dmitry Guest

    Hi all,

    Consider the following code:
    class A {
    public:
    A() {}
    void DoMethod() { (this->*m_pMethod)(); }

    protected:
    virtual void MethodA() { ... }
    virtual void MethodB() { ... }
    void (A::*m_pMethod)();
    };


    class B1 : public A {
    public:
    B1() { m_pMethod = &A::MethodA; } // invokes B1::MethodA()
    protected:
    virtual void MethodA() { ... }
    };

    Class A has a member m_pMethod, which is pointer to a class member
    function. This function can be over ridden by derived class (in my
    example this is what B1 does). There more B2....Bn in the system.

    This code was fine under gcc 3.2.3 but not ok under 4.1.2:
    test.cc: In constructor 'B1::B1()':
    test.cc:9: error: 'virtual void A::MethodA()' is protected
    test.cc:17: error: within this context

    Ok, after some googling I found it's related to gcc 3.4 changes -
    http://www.gnu.org/software/gcc/gcc-3.4/changes.html#3.4.6 (look for
    "forming a pointer to member or a pointer to member function").

    Setting B1() { m_pMethod = &B::MethodA; } does not help too:
    test.cc: In constructor 'B1::B1()':
    test.cc:17: error: cannot convert 'void (B1::*)()' to 'void (A::*)()'
    in assignment

    Any ideas how to fix it?

    BR
    Dima
    Dmitry, Jun 9, 2008
    #1
    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. Shelly Adhikari
    Replies:
    3
    Views:
    356
    red floyd
    Sep 10, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    790
    Micah Cowan
    Jan 12, 2005
  3. Andy Lomax
    Replies:
    5
    Views:
    453
    John Carson
    Jun 30, 2005
  4. Dmitry
    Replies:
    2
    Views:
    774
    James Kanze
    Jun 10, 2008
  5. blangela
    Replies:
    8
    Views:
    651
    Erik Wikström
    Sep 26, 2008
Loading...

Share This Page