Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to Member Functions and Inheritance

Reply
Thread Tools

Pointer to Member Functions and Inheritance

 
 
none
Guest
Posts: n/a
 
      08-07-2004
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 type) in the derived
class. I know in Visual C++ you can just cast the
pointer to member function of the derived class to
that of the base class and it will work, but is this
legal by C++ standards?

Thanks for any replies,
steve

Here's a sample source.

#include<iostream>
using namespace std;

class base {
protected :
// pointer to member function variable
typedef void (base::*function)(void);
function ptr;
public :
// call the member function addressed by ptr
virtual void CallFunction(void) { (this->*ptr)(); }
public :
// possible functions to call
virtual void Function1(void) {
cout << "Calling base::Function1..." << endl;
}
virtual void Function2(void) {
cout << "Calling base::Function2..." << endl;
}
public :
base() { ptr = &base::Function1; }
virtual ~base() { ptr = 0 };
};

class derived : public base {
public :
void Function1(void) {
cout << "Calling derived::Function1..." << endl;
}
void Function2(void) {
cout << "Calling derived::Function2..." << endl;
}
public :
derived() {
// must cast member to base type, this legal?
ptr = (function)&derived::Function1;
}
~derived() {}
};

int main()
{
derived d;
d.CallFunction();
return 0;
}

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      08-08-2004
On Sat, 07 Aug 2004 23:36:57 GMT, none <> wrote:

>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 type) in the derived
>class. I know in Visual C++ you can just cast the
>pointer to member function of the derived class to
>that of the base class and it will work, but is this
>legal by C++ standards?


You shouldn't need a cast, if I am reading the standard correctly. If
the member is accessible (i.e. public or protected), then you should
be able to use it directly.

If a cast is necessary, you must cast the base class pointer to that
of the derived class according to 4.11.2 of the standard. The base
class may not be inaccessible, nor ambiguous, nor virtual.

There is a footnote here which reads:

"The rule [...] appears inverted compared to the rule for pointers to
objects [...] This inversion is necessary to ensure type safety [...]"

--
Bob Hairgrove

 
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
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Predefined class member functions and inheritance lovecreatesbeauty C++ 3 05-19-2005 03:34 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments