473,748 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why parent classes' vptrs are stored in derived objects

Hi All,
Consider the following scenario:
class Top { };
class Left: virtual public Top { };
class Right: virtual public Top { };
class Bottom: public Left, public Right {};

Many books propose that object of Bottom contains three vptrs, one for
Left and Bottom, one for Right and one for Top.
Now my question is why the compiler is storing superclasses' vptrs in
derived class object. After all Bottom also has a vptr pointing to its
vtable from which it will call all virtual functions.
Thanks in advance.

Feb 27 '07 #1
4 3632

Ki**********@gm ail.com wrote:
Hi All,
Consider the following scenario:
class Top { };
class Left: virtual public Top { };
class Right: virtual public Top { };
class Bottom: public Left, public Right {};

Many books propose that object of Bottom contains three vptrs, one for
Left and Bottom, one for Right and one for Top.
Now my question is why the compiler is storing superclasses' vptrs in
derived class object. After all Bottom also has a vptr pointing to its
vtable from which it will call all virtual functions.
Thanks in advance.
Well as far as you use derived object alone it works fine. But what
happens when you dynamic cast Bottom pointer to either of Left, Right
or Top like:
Bottom * b = new Bottom;
Left * something = <dynamic_cast*> b;
In this case you type casting you should use something as if it is a
original Left object; for that you need to access Left class' vtbl.
Thats why you need to store base classes vptrs in the derived objects.
Hope its clear.

Feb 27 '07 #2
ud************@ gmail.com wrote:
Ki**********@gm ail.com wrote:
>>Hi All,
Consider the following scenario:
class Top { };
class Left: virtual public Top { };
class Right: virtual public Top { };
class Bottom: public Left, public Right {};

Many books propose that object of Bottom contains three vptrs, one for
Left and Bottom, one for Right and one for Top.
Now my question is why the compiler is storing superclasses' vptrs in
derived class object. After all Bottom also has a vptr pointing to its
vtable from which it will call all virtual functions.
Thanks in advance.


Well as far as you use derived object alone it works fine. But what
happens when you dynamic cast Bottom pointer to either of Left, Right
or Top like:
Bottom * b = new Bottom;
Left * something = <dynamic_cast*> b;
In this case you type casting you should use something as if it is a
original Left object; for that you need to access Left class' vtbl.
Thats why you need to store base classes vptrs in the derived objects.
Hope its clear.
No that's not clear. When calling a method from a Left*, if it points to
a Bottom, then Bottom's virtual functions should be used, not Left's.

--
Ron House ho***@usq.edu.a u
http://www.sci.usq.edu.au/staff/house
Mar 1 '07 #3
Yes, you are right Ron. Bit misunderstood the question.
May be this explanation is clear:

class D : public A, public B, public C {.........}

If we dont store the base classes' vptrs in derived objects, that
means you have to club all the virtual functions of all the base
classes (in case of multiple inheritance) into one vtbl that has a
single vptr that will be stored in your object. Now, no problem in
accessing virtual functions of first subobject (like class A in the
above code snippet) whose virtual function pointers are stored in
starting location of vtbl. But what happens in accessing other
subobjects (like classes B and C in the above code snippet) virtual
functions. Their offsets will get disturbed as you have clubbed all
the virtual functions into one vtbl. After all, compiler will access
virtual functions with their offsets only. Now you dont know what will
be get called.

Thats why in case of single or pure multilevel inheritance you dont hv
to store the base classes' vptrs in derived objects.

Now the question: Can not compiler determine these changes in offsets
and adjust virtual functions pointers accordingly? It may, it depends
on the compiler. But most of the compilers adopt this strategy and may
some compilers do this optimization.

Mar 1 '07 #4
ud************@ gmail.com wrote:
Yes, you are right Ron. Bit misunderstood the question.
May be this explanation is clear:

class D : public A, public B, public C {.........}

If we dont store the base classes' vptrs in derived objects, that
means you have to club all the virtual functions of all the base
classes (in case of multiple inheritance) into one vtbl that has a
single vptr that will be stored in your object. Now, no problem in
accessing virtual functions of first subobject (like class A in the
above code snippet) whose virtual function pointers are stored in
starting location of vtbl. But what happens in accessing other
subobjects (like classes B and C in the above code snippet) virtual
functions. Their offsets will get disturbed as you have clubbed all
the virtual functions into one vtbl. After all, compiler will access
virtual functions with their offsets only. Now you dont know what will
be get called.

Thats why in case of single or pure multilevel inheritance you dont hv
to store the base classes' vptrs in derived objects.

Now the question: Can not compiler determine these changes in offsets
and adjust virtual functions pointers accordingly? It may, it depends
on the compiler. But most of the compilers adopt this strategy and may
some compilers do this optimization.
Yes, with multiple inheritance, obviously A, B, and C cannot all share
the same starting address as D, so yes, there must be an offset on the
address of the _data_ of the object, when calling superclass functions
(of all but 1); and there must be distinct vtable addresses for all but
1 also. But like you, I cannot think why this cannot be calculated. My
only suggestion is that perhaps it is a performance issue. But even
here, surely all such objects will have the same relationships, so the
extra addresses could be stored in the vtable, I would have thought.

We must be overlooking something (unless the book authors are)!

--
Ron House ho***@usq.edu.a u
http://www.sci.usq.edu.au/staff/house
Mar 2 '07 #5

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

Similar topics

16
2672
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
9
5122
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In half-code this should in the end look like this: In the child class:
4
22211
by: Davey | last post by:
I have a website which has a popup window (this only opens when the user chooses to open it). In the popup window I have a <select> control which lists a selection of "classes". Each class has a description and a class_id (stored in the value attribute of each option). The user will then select a class from the drop-down list. What I want to do is have a control in the parent browser window which can store the class_id and the...
8
18589
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the derived classes, but I can use them only calling the methods declared in abstract class (the new methods declared only in derived classes return an error). Please take a look to my code (hey, this is not an homework!
5
2006
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1" Gpa="3.50"> <Person Id="1" FirstName="FirstName0" LastName="LastName0" MiddleInitial="W"/> </Student> In my code, person is the base class and student extends it. When I
11
2876
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class called animal, and from within animal you can access lots more classes such as feline, canine, reptile and amphibian.....
6
6081
by: reandeau | last post by:
I'm building out a OO based app in PHP 5 but I'm getting a little confused on children contructing parents. I have a parent that looks like this: abstract Class State { protected $database; protected $user; protected $output; public function __construct($database,$user,$output) { $this->database = $database;
17
3543
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
2
2412
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
0
8996
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
9562
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...
1
9333
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,...
0
9254
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
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
4608
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
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.