473,378 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 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
Jun 27 '08 #1
3 3152
Dmitry wrote:
Hi all,

Consider the following code:
[..]
Please have patience. If somebody knows the answer and is willing to
help, 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
Jun 27 '08 #2
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
Jun 27 '08 #3
On Jun 9, 2:39 pm, Dmitry <dim...@gmail.comwrote:
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:ja*********@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
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: none | last post by:
Hi, I have a base class with a pointer-to-member function variable. Then I have a derived class that needs to use that variable to call a member function (with the same arguments and return value...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
2
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
0
by: Dmitry | last post by:
Hi all, Consider the following code: class A { public: A() {} void DoMethod() { (this->*m_pMethod)(); } protected: virtual void MethodA() { ... }
3
by: Dan Smithers | last post by:
I want to implement a C++ wrapper to C code that requires a function pointer to be passed in. Specifically, I want a wrapper for pthread that clients can use as a base class. Is there a way of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.