Re: Non-virtual with space for vtable ptr?
"?? Tiib" <ootiib@hot.ee> schrieb im Newsbeitrag
news:856cac02-9d07-4d3d-9920-6c270b31cf63@n10g2000yqf.googlegroups.com...
On Apr 30, 3:17 am, "Matthias Hofmann" <hofm...@anvil-soft.com> wrote:
"?? Tiib" <oot...@hot.ee> schrieb im
Newsbeitragnews:ed63acc7-6d73-4280-ac80-578142dde0a8@e21g2000yqe.googlegroups.com...
I can't see how implementation can use something that is located at
end of the object. Implementation would be in difficulties to find the
end.
The vtable pointer is just an invisible class member. Why should it be
harder to locate than any other class member, no matter what its offset
within the class might be?
When vtable* is invisible first class member (at offset 0) then with
minimum information (having void* to object) run time the program can
cast it into vtable* and derive the final type, the size, pointers to
all its virtuals, etc. of pointed at object.
If the vtable* was located at some other fixed offset like 100 it
would also work but then every object should have at least such size.
The vtable could be at offset 0 for some classes and at offset 8 for other
classes. It does not have to be the same for every class, a compiler might
decide to put the vtable pointer after the first two members for classes
that have two or more members.
Deciding where is *last* member of object would be impossible for
implementation having just void* to object and nothing at start (or
other fixed offset).
You do not have an object if you have a void pointer. You might as well look
for the vtable pointer within an int or a double.
On the other hand if there has to be something at
start anyway then why not vtable* itself?
It's probably a good place to store a vtable pointer, but the compiler is
not required to store it there. One day somebody might develop a CPU with
hardware support for polymorphism that stores meta information including
vtables for each memory location within the virtual address space, and on
such a machine the compiler would not have to deal with vtables at all.
Nothing of it is specified in standard (even existence of vtable*),
but i trust that what i describe is quite usual solution.
Yes, that's true.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]