473,513 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to Member Functions and Inheritance

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;
}

Jul 22 '05 #1
1 1658
On Sat, 07 Aug 2004 23:36:57 GMT, none <no**@none.com> 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
No**********@Home.com
Jul 22 '05 #2

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

Similar topics

2
1667
by: Thomas Matthews | last post by:
Hi, I have a hierarchy of classes and would like to create an array of pointers to member functions that could also contain pointers to the parent member functions as well. What is the syntax for this? #include <string> using std::string;
15
2741
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes as member variables. So including a forward declaration doesnt help, does it? Faced with these, I had the following options: -Include the...
8
1843
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On what arguments do you decide whether to pass a reference to the object of class B to the member function like this operator()(classB& objB) or to have...
37
4946
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well...
22
2716
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
16
15619
by: Alex Vinokur | last post by:
Does it have to be? : sizeof (size_t) >= sizeof (pointer) Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
5
3618
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer...
3
1435
by: Rahul | last post by:
While reading Inside the C++ object model I came through the following paragraph Point3d origin, *ptr = &origin; A) origin.x = 0.0; B) ptr->x = 0.0; The book says "A & B statements performs equivalently if x is a member of a struct, class, single inheritance hierarchy, or multiple inheritance hierarchy" This is because compiler knows...
7
3795
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.