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

Home Posts Topics Members FAQ

v-pointer in a class/object?

I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?

Thanks

Sep 17 '07 #1
6 4846
puzzlecracker wrote:
I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?
An implementation is not required to provide a vptr or a vtbl. Most
implementations use them, but they are not required to.

If your implementation uses vptr, it would be the natural pointer size
for your platform (most likely *NOT* 16 bits), and each object of that
class would have the vptr. There would be one vtbl for each class, and
each object of that class would point to that vtbl.

For details of how a vptr/vtbl model works, see Lippman's "Inside the
C++ Object Model".
Sep 17 '07 #2
On Sep 16, 8:29 pm, red floyd <no.s...@here.d udewrote:
puzzlecracker wrote:
I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?

An implementation is not required to provide a vptr or a vtbl. Most
implementations use them, but they are not required to.

If your implementation uses vptr, it would be the natural pointer size
for your platform (most likely *NOT* 16 bits), and each object of that
class would have the vptr. There would be one vtbl for each class, and
each object of that class would point to that vtbl.

For details of how a vptr/vtbl model works, see Lippman's "Inside the
C++ Object Model".
Seems like an overhead, to have one pointer per object, that can
possibly be avoided. All object pointers to a specific derived type
have pointer to the same virtual table. Perhaps we can store that
pointer (as static variable or external) that each object of that
derived class can see/use. Is such construct available or can be
simulate it in C++? Agh, it is really a compiler issue.

Thanks

Sep 17 '07 #3
"puzzlecrac ker" <ir*********@gm ail.comwrote in message
news:11******** *************@2 2g2000hsm.googl egroups.com...
>I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?
You can take a look at the following code for an example minimalist
implementation of vtables in C:

http://groups.google.com/group/comp....106926ba5db19f

This shows the internals of a very-basic abstract interface technique for C.
It might help you understand the that a possible C++ implementation might
use one per-object pointer to a single per-class static/constant vtable...

Sep 17 '07 #4
"puzzlecrac ker" <ir*********@gm ail.comwrote in message
news:11******** *************@2 2g2000hsm.googl egroups.com...
>I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?
For alignment well, it depends on how the implementation sets things up.
Sometimes, the pointer to the vtable is placed directly at the front of the
common data-structure which makes up object's of a particular class. So the
subsequent object will be aligned on at least a boundary sufficient enough
to hold that vtable pointer...

Sep 17 '07 #5
On Sep 17, 2:43 am, puzzlecracker <ironsel2...@gm ail.comwrote:
On Sep 16, 8:29 pm, red floyd <no.s...@here.d udewrote:
puzzlecracker wrote:
I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?
An implementation is not required to provide a vptr or a vtbl. Most
implementations use them, but they are not required to.
If your implementation uses vptr, it would be the natural pointer size
for your platform (most likely *NOT* 16 bits), and each object of that
class would have the vptr. There would be one vtbl for each class, and
each object of that class would point to that vtbl.
For details of how a vptr/vtbl model works, see Lippman's "Inside the
C++ Object Model".
Seems like an overhead, to have one pointer per object, that can
possibly be avoided. All object pointers to a specific derived type
have pointer to the same virtual table. Perhaps we can store that
pointer (as static variable or external) that each object of that
derived class can see/use. Is such construct available or can be
simulate it in C++? Agh, it is really a compiler issue.
And given a pointer or a reference to the base class, how does
the compiler know which static variable to use? Other solutions
are possible; the compiler could keep a hash map mapping Base*
to vtable*, for example. But they also involve extra memory per
object; more, in fact, than just a single pointer.

The basic problem, of course, is that you do need extra
information, at run-time. And storing extra information means
using extra bits. Globally, using a vptr in the object seems to
be the most efficient solution, both in terms of space and
execution time.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 17 '07 #6
On 2007-09-17 02:43, puzzlecracker wrote:
On Sep 16, 8:29 pm, red floyd <no.s...@here.d udewrote:
>puzzlecracke r wrote:
I am kind of confused with the vpointer. Assuming the class has a
virtual function. Would ONE pointer (occupying 16 bits) to virtual
table be create for the class or One for each object? How does this
pointer fit into aligment properties of an object (of the class) or
class itself?

An implementation is not required to provide a vptr or a vtbl. Most
implementation s use them, but they are not required to.

If your implementation uses vptr, it would be the natural pointer size
for your platform (most likely *NOT* 16 bits), and each object of that
class would have the vptr. There would be one vtbl for each class, and
each object of that class would point to that vtbl.

For details of how a vptr/vtbl model works, see Lippman's "Inside the
C++ Object Model".

Seems like an overhead, to have one pointer per object, that can
possibly be avoided. All object pointers to a specific derived type
have pointer to the same virtual table. Perhaps we can store that
pointer (as static variable or external) that each object of that
derived class can see/use. Is such construct available or can be
simulate it in C++? Agh, it is really a compiler issue.
How would the object see such a variable? Either by having a pointer to
it (same overhead but one extra indirection) or by storing that
information together with the type. The problem with the second is that
the type then would have to be known at compile-time, in which case you
no longer have run-time polymorphism and not much need for virtual
functions at all.

--
Erik Wikström
Sep 17 '07 #7

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

Similar topics

24
6746
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or with "first-class airplane ticket". I just find the name a bit strange. Also, if there are first-class objects, what would the second-class objects or economy/tourist class objects be? :)
2
1765
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a = 5 Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'object' object has no attribute 'a'
106
5579
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the same as an assignment, but for class types it has a different effect - it calls the copy constructor. My question is when to not use an initalisation list for initialising data members of a class?
1
2180
by: Zhongqi Pan | last post by:
I am facing a problem- I am using f1(this) to pass a class object pointer to a function ( say f1) f1 is defined as follows
9
4804
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
1
16240
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition to the fact1 is given as since the derived object always consists of the base part , the base class pointer will always point to the base part in the derived object unless otherwise the function in the base class are declared as virtual and are...
6
1277
by: Brad | last post by:
I am creating a class (not a control) which implements IStateManager. I've created the class and all of the implementations (LoadView, SaveViewState, etc...). My Question is: How do I instantiate this class in a page and then re-instantiate it on post back such that I don't lose it's viewstate? If the page always uses x = new myclass to instantiate, the myclass constructor is always going to reinitialize its statebag.
2
2342
by: Steve | last post by:
Hello, If I instantiate a class object from a form I would like to be able to write to a label on the calling form something like "hello from class object" from within a subroutine inside the class object. Is this possible?
4
5247
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not attempt to access the object -- since m_object is not actually created and initizialized until after the base constructor has been called. Any thoughts on the practice below? class Base { public:
13
4929
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
0
9386
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
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
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
1
3319
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
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.