Dmitry wrote:
> 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-...ges.html#3.4.6 (look for
> "forming a pointer to member or a pointer to member function").
Other compilers agree with this. You can only take the address of
protected members of the base class, not of any other A's.
>
> Setting B1() { m_pMethod = &B1::MethodA; } does not too:
> test.cc: In constructor 'B1::B1()':
> test.cc:17: error: cannot convert 'void (B1::*)()' to 'void
> (A::*)()' in assignment
It s in that it takes the address of a function. Just not the
right one. :-)
>
> Any ideas how to fix it?
>
Why is B1 setting a variable in A? Can you move that to a member
function of A (and make the pointer private)?
> class A {
> public:
> A() {}
> void DoMethod() { (this->*m_pMethod)(); }
>
> protected:
> virtual void MethodA() { ... }
> virtual void MethodB() { ... }
void SelectMethodA()
{ m_pMethod = &A::MethodA; }
private:
> void (A::*m_pMethod)();
>
> };
>
> class B1 : public A {
> public:
B1() { SelectMethodA(); }
> protected:
> virtual void MethodA() { ... }
>
> };
Bo Persson