473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning ptr to a virtual member function of a base class

Consider the following example:

---

#include <iostream>

class Base
{
public:
virtual void fn() { std::cout << "Base::fn()\n"; }
};

typedef void (Base::*Base_fn_ptr)();

class Derived: public Base
{
public:
virtual void fn() { std::cout << "Derived::fn()\n"; }

static Base_fn_ptr Get_Base_Fn_Ptr() { return &Base::fn; }
};

int main()
{
Derived obj;
Base_fn_ptr ptr = Derived::Get_Base_Fn_Ptr();
(obj.*ptr)();

return 0;
}

---

Running the example causes "Derived::fn()" to be output. Is there a way
to craft Get_Base_Fn_Ptr() so that it returns the address of Base::fn()
instead of a virtual function-calling stub?

In other words, I want to defeat the polymorphic behavior of obj.

- Dave Rahardja
Jul 22 '05 #1
3 1255
If I understand what you need, the way to defeat the polimorfic
behaviour of obj is to use the explicit qualification mechanism in
calling fn.

In your code you only have to call:

obj.Base::fn(); // explicit call to Base fn()
obj.Derived::fn(); // explicit call to Derived fn()

Gianguglielmo

Jul 22 '05 #2
Dave Rahardja wrote:
Consider the following example:

---

#include <iostream>

class Base
{
public:
virtual void fn() { std::cout << "Base::fn()\n"; }
};

typedef void (Base::*Base_fn_ptr)();

class Derived: public Base
{
public:
virtual void fn() { std::cout << "Derived::fn()\n"; }

static Base_fn_ptr Get_Base_Fn_Ptr() { return &Base::fn; }
};

int main()
{
Derived obj;
Base_fn_ptr ptr = Derived::Get_Base_Fn_Ptr();
(obj.*ptr)();

return 0;
}

---

Running the example causes "Derived::fn()" to be output. Is there a way
to craft Get_Base_Fn_Ptr() so that it returns the address of Base::fn()
instead of a virtual function-calling stub?

In other words, I want to defeat the polymorphic behavior of obj.


By using pointer to member functions, no. This system was especially
made for these cases: to make sure wherever in the hierarchy you take an
address, the correct function gets called.

You are probably aware you can call it directly by qualifying the function :

obj->Base::fun();
However, look at that :

# include <iostream>

class Base
{
public:
void f()
{
std::cout << "in base::f()";
}

virtual void vf()
{
f();
}
};

class Derived : public Base
{
public:
virtual void vf()
{
std::cout << "in derived::vf()";
}
};
typedef void (Base::*Pf)();

Pf test1()
{
return &Base::f;
}

Pf test2()
{
return &Base::vf;
}

int main()
{
Base *b = new Derived;

Pf pf = test1();
(b->*pf)(); // in derived::vf()

pf = test2();
(b->*pf)(); // in base::vf()

}

This may or not be what you look for.
Jonathan
Jul 22 '05 #3
GianGuz wrote:
If I understand what you need, the way to defeat the polimorfic
behaviour of obj is to use the explicit qualification mechanism in
calling fn.

In your code you only have to call:

obj.Base::fn(); // explicit call to Base fn()
obj.Derived::fn(); // explicit call to Derived fn()

Gianguglielmo


That's exactly what I'm looking for. Thanks! I guess the syntax of the
explicit call escaped me.

-dr
Jul 22 '05 #4

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

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
6
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.