PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Pointer to protected member function from derived class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Pointer to protected member function from derived class

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 13h39   #1
Dmitry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pointer to protected member function from derived class

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 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
  Réponse avec citation
Vieux 09/06/2008, 13h49   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to protected member function from derived class

Dmitry wrote:
> Hi all,
>
> Consider the following code:
> [..]


Please have patience. If somebody knows the answer and is willing to
, they'll reply. No need to post your inquiry again within 10
minutes of the original post *unless* you provide more information
(which you don't seem to here).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  Réponse avec citation
Vieux 09/06/2008, 18h09   #3
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to protected member function from derived class

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


  Réponse avec citation
Vieux 10/06/2008, 09h16   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer to protected member function from derived class

On Jun 9, 2:39 pm, Dmitry <dim...@gmail.com> 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 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 er object (possibly friend) in A which calls the
desired fucntion and which can be instantiated in B.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h10.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12613 seconds with 12 queries