473,624 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual function pointers (& multiple inheritance?)

Hi all. I'm maintaining a C++ program and I've come across a nasty piece
of code that works, but I just don't understand why. I'm not actually
this part of the program, but I really want to know how and why it works.

I'll post a simplified version of it below and ask my questions
afterwards:

class Base {
void *function_ptr;

public:
Base();
void Initialize();
virtual void Function();
};

Base::Base() {
function_ptr = (void *)&Base::Functi on;
}

void Base::Initializ e() { // should be called from derived constructor
if ( function_ptr != (void *)&Base::Functi on ) {
/* do something */
}
}

void Base::Function( ) {
/* some code */
}
class Derived : public virtual Base {
public:
Derived();
virtual void Function();
};

Derived::Derive d() {
Initialize();
}

void Derived::Functi on() {
/* some code */
}
So the derived class overrides the virtual function "Function". Now, when
the derived class calls Initialize (which is non-virtual and defined in
the base), the statements between the curly braces in the if clause
actually get executed. Why does this work? It seems like the base is
setting function_ptr to &Base::Function , but then when it checks it again
in Initialize(), they're no longer equal.

Does this have anything at all to do with the fact that Base is a virtual
base class (ie would this still work if it wasn't a virtual base)? As far
as I knew, virtual bases are used only to prevent multiple copies of the
base class in a multiple inheritance tree, but recent discussion in this
newsgroup has caused me to wonder if there isn't (quite a bit) more to it
than that.

Thanks in advance for any help.

*************** *************** *************** ********
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
*************** *************** *************** ********

Jul 19 '05 #1
11 8092

"Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message
news:Pi******** *************** *************** @plg2.math.uwat erloo.ca...
function_ptr = (void *)&Base::Functi on;


This isn't a legitmate cast. No guarantee that a member function pointer can be cast
to void* safely.

What are you trying to do?

Jul 19 '05 #2
Josh Lessard wrote:
...
I'm maintaining a C++ program and I've come across a nasty piece
of code that works, but I just don't understand why. I'm not actually
this part of the program, but I really want to know how and why it works.
...


There's no way to say why it works. Your code is broken (see Ron's
reply) and its behavior is undefined. It might appear to be "working" if
you are lucky and if the weather is right. And tomorrow it might start
to crash consistently.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3
Ron Natalie wrote:
"Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message
news:Pi******** *************** *************** @plg2.math.uwat erloo.ca...

function_ptr = (void *)&Base::Functi on;

This isn't a legitmate cast. No guarantee that a member function pointer can be cast
to void* safely.

What are you trying to do?


Isn't it obvious?

Someone has exploited a compiler bug to determine whether
Initialize is being called in the constructor of Base or
not. The comment the idiot left says as much.

Jul 19 '05 #4
lilburne wrote:
Ron Natalie wrote:
"Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message
news:Pi******** *************** *************** @plg2.math.uwat erloo.ca...

function_ptr = (void *)&Base::Functi on;


This isn't a legitmate cast. No guarantee that a member function
pointer can be cast
to void* safely.

What are you trying to do?


Isn't it obvious?

Someone has exploited a compiler bug to determine whether Initialize is
being called in the constructor of Base or not. The comment the idiot
left says as much.


PS: Your compiler is converting the cast to be an actual
address (BTW this wont compile on some other compilers). In
the constructor of Base this is taken as the actual address
of Base::Function, but in the Initialize method it is
probably being read from the vtable.

Try testing it to see what happens if you call Initialize
from Base constructor or with a class that doesn't override
Function.

Jul 19 '05 #5
> > function_ptr = (void *)&Base::Functi on;

This isn't a legitmate cast. No guarantee that a member function pointer can be cast
to void* safely.
In this case, it doesn't have to be a "safe cast". The sole purpose of
function_ptr is to compare it to the Base::Function address in the
Initialize() function to see if the function has been overridden in a
derived class.

What it appears to be doing is assigning &Base::Funct ion into function_ptr
in the constructor, and then comparing function_ptr to &Base::Funct ion in
Initialize(). What throws me off kilter is that if Base::Function has
actually been overridden in Derived, they're no longer equal. Why not?
What are you trying to do?


I didn't write the code. I'm extending a project that was written back in
1997. As I said in my earlier post, I'm not touching this code...I'm just
very curious as to why it works.

What the original coder was trying to do was check to see if a derived
class has overridden any of the virtual functions in the base class. His
method works...I'm just at a loss to understand why.

*************** *************** *************** ********
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
*************** *************** *************** ********

Jul 19 '05 #6
> In this case, it doesn't have to be a "safe cast".

The above sentence one of those things I wish I could have taken back just
after I'd sent it. Believe me, I understand what was meant when you said
it wasn't a safe cast. I agree with you...the code is pretty ugly in
places.

All I meant is that nothing is really done with the function pointer,
except to compare it to the address of the function in the base class
(which is where it was taken from anyway).

In any case, it appears that this is working by pure fluke...so I don't
feel so bad that I didn't understand it when I came across it.

Thanks for your replies!

*************** *************** *************** ********
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
*************** *************** *************** ********

Jul 19 '05 #7

"Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message
news:Pi******** *************** **************@ plg2.math.uwate rloo.ca...
function_ptr = (void *)&Base::Functi on;
This isn't a legitmate cast. No guarantee that a member function pointer can be cast
to void* safely.


In this case, it doesn't have to be a "safe cast".


Safe doesn't enter into it. The compiler doesn't have to even accept it.
No where in the docs for reinterpret_cas t does it say you can cast a pointer
to member function to void*. In many implementations they aren't even the
same size.
The sole purpose of
function_ptr is to compare it to the Base::Function address in the
Initialize() function to see if the function has been overridden in a
derived class.
Yes and it only coincidentally works for that. As I said, on many implementations
the cast won't even compile. On others, it may not give you the same prediction.
What the original coder was trying to do was check to see if a derived
class has overridden any of the virtual functions in the base class. His
method works...I'm just at a loss to understand why.

It's pure coincidence. You can't support that code. Once the compiler changes
it may cease to work suddenly. It's dubious that it works at all.
Jul 19 '05 #8

"Josh Lessard" <jr******@plg2. math.uwaterloo. ca> wrote in message
news:Pi******** *************** **************@ plg2.math.uwate rloo.ca...

All I meant is that nothing is really done with the function pointer,
except to compare it to the address of the function in the base class
(which is where it was taken from anyway).

You're not even guaranteed that. void* isn't a parking place for an arbitrary
pointer, it's a parking place for an arbitrary object pointer. Pointers to functions
and any sort of pointer to member are not validly converted to void*. Many
compilers will refuse to even attempt such a conversion.
Jul 19 '05 #9
On Thu, 6 Nov 2003, Ron Natalie wrote:
It's pure coincidence. You can't support that code. Once the compiler changes
it may cease to work suddenly. It's dubious that it works at all.


I agree with you 100%. Your answers here have just confirmed my
suspicions, and I don't feel so bad for not understanding what the hell
was going on and why it was "working." :P

Maybe it's time I ordered myself a copy of the C++ standard. I've been
picking up bits and pieces of it by lurking around in this newsgroup, but
it would be nice to be able to confirm things like this for myself.

Thanks again for your time.

*************** *************** *************** ********
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
*************** *************** *************** ********

Jul 19 '05 #10

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

Similar topics

2
8024
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
4
4423
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with one vtbl ptr BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes . class A = 4 bytes class B :public A = 4 bytes class...
4
4639
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived class version from the base class pointer and still be able to use a vector with derived element pointers? class BaseElem { }; class DerivedElem1 { };
18
2207
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
2889
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
11
4347
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 it inline. Like this.
7
1921
by: alex.mizrahi | last post by:
helo i'm going to use pointers to member function with virtual inheritance in following way: struct base { public: typedef void (base::*fun_t)(void); int i; void call(fun_t f) {
3
4545
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
23
4599
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
8240
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6111
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
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
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.