Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to protected member function from derived class

Reply
Thread Tools

Pointer to protected member function from derived class

 
 
Dmitry
Guest
Posts: n/a
 
      06-09-2008
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").

Setting B1() { m_pMethod = &B1::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
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      06-09-2008
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 help too:
> test.cc: In constructor 'B1::B1()':
> test.cc:17: error: cannot convert 'void (B1::*)()' to 'void
> (A::*)()' in assignment


It helps 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


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-10-2008
On Jun 9, 2:39 pm, Dmitry <(E-Mail Removed)> wrote:
> 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 = &B1::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?


Make the functions public. (I find almost no use for protected
anyway, except in closed hierarchies, and then, it's almost
always for data, and not functions.)

Provide a protected function in A which returns the desired
pointer to member. (Note that it cannot be a static function,
even though you don't access any members of the object, or
you'll run into the same problem. I think.) Or provide a
protected function which sets the member pointer to function to
the desired value.

Provide an additional virtual function in A which is overloaded
in the derived class to dispatch to whichever function is
desired.

Provide a helper object (possibly friend) in A which calls the
desired fucntion and which can be instantiated in B.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
Why can derived member function not access protected member of a baseclass object? blangela C++ 8 09-26-2008 03:24 PM
Pointer to protected member function from derived class Dmitry C++ 0 06-09-2008 12:30 PM
When is a base class protected member not visible in a derived class? Andy Lomax C++ 5 06-30-2005 04:34 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Is this valid C++ (protected member of base accessed in derived class function) Shelly Adhikari C++ 3 09-10-2003 08:18 PM



Advertisments