473,388 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 4822
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.dudewrote:
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
"puzzlecracker" <ir*********@gmail.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.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
"puzzlecracker" <ir*********@gmail.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.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...@gmail.comwrote:
On Sep 16, 8:29 pm, red floyd <no.s...@here.dudewrote:
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 objektorientierter Datenverarbeitung
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.dudewrote:
>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.
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
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...
2
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...
106
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...
1
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
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...
1
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...
6
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...
2
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...
4
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...
13
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...
0
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...

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.