Unique VTABLE

C

CoDE++

Every object of Polymorphic class, contains a Virtual pointer ( which
points to Vtable).

If I have 1000 objects of a polymorphic class,
why should i waste 1000*4 bytes for storing the same stuff...

All 1000 object contains the exatly same value of virtual pointer...

so the question is why can't this virtual pointer could have been kept
in class by complier as it keep all the member function definition
somewhere... If a class A has member function func1().. there is only
one copy of it ...for all the objects... then why do we have separate
copy of virtual pointer for each object...while value is same in all
the objects ?

How do you think compiler finds the address of a member function for a
non polymorphic class?

Don't you think the same mechanism could have been used to find out
the virtual pointer for that class also?
 
F

Francesco S. Carta

Every object of Polymorphic class, contains a Virtual pointer ( which
points to Vtable).

If I have 1000 objects of a polymorphic class,
why should i waste 1000*4 bytes for storing the same stuff...

All 1000 object contains the exatly same value of virtual pointer...

so the question is why can't this virtual pointer could have been kept
in class by complier as it keep all the member function definition
somewhere... If a class A has member function func1().. there is only
one copy of it ...for all the objects... then why do we have separate
copy of virtual pointer for each object...while value is same in all
the objects ?

How do you think compiler finds the address of a member function for a
non polymorphic class?

Don't you think the same mechanism could have been used to find out
the virtual pointer for that class also?

I think that's one the implementers' affairs, but if you're really in to
criticize it you should be working out the details by yourself and
eventually also propose a change illustrating how your solution reduces
the memory consumption, when compared to the current implementation.

I find it perfectly reasonable for a polymorphic object to bring around
additional data that ensures I will be able to know what exact class it
was at its origins, after that I passed it around as a pointer or as a
reference to one of its base classes.

Besides, after writing the above, a doubt raised into my mind... what
does the standard tell about them?

<quote>
1.8 The C++ object model
Some objects are polymorphic (10.3); the implementation generates
information associated with each such object that makes it possible to
determine that object’s type during program execution. For other
objects, the interpretation of the values found therein is determined by
the type of the expressions (clause 5) used to access them.
</quote>

The implementation is expected to use an unspecified amount of
additional storage for polymorphic objects.

So then, maybe, that 1000*4 can be shrunk... try to find a way,
implementers would find that quite useful - besides, some
implementations could very well store 1000*8, as I heard... these would
be even more happy with your optimizations.
 
Ö

Öö Tiib

Every object of Polymorphic class, contains a Virtual pointer ( which
points to Vtable).

Standard does not require it, but this is common way to implement it.
If I have 1000 objects of a polymorphic class,
why should i waste 1000*4 bytes for storing the same stuff...

Yes, if you have them all of same class then it is not polymorphism.
It is called member functions were made worthlessly virtual.
Polymorphism is when you have 1000 pointers to base and behind these
may be are objects of different classes from same polymorphic
inheritance hierarchy.
All 1000 object contains the exatly same value of virtual pointer...

.... that was made virtual without reason.
so the question is why can't this virtual pointer could have been kept
in class by complier as it keep all the member function definition
somewhere... If a class A has member function func1().. there is only
one copy of it ...for all the objects... then why do we have separate
copy of virtual pointer for each object...while value is same in all
the objects ?

Compilers are doing extremely fine job these days. Sometimes it is
good idea to compile someones code under minimize size optimizations
to turn it into readable piece of assembly at least. Please, first
tell us the reason why the developer made it virtual???
How do you think compiler finds the address of a member function for a
non polymorphic class?

Depends on situation. In some situations it does not even know it.
Compiler may mark it down and linker may tie the addresses together.
Don't you think the same mechanism could have been used to find out
the virtual pointer for that class also?

!!!Brain teaser alert!!! Why you think the language does allow to
declare a member function both inline and virtual? If you can answer
it fully then ... you hopefully stop underestimating compilers.
 
J

Jorgen Grahn

Every object of Polymorphic class, contains a Virtual pointer ( which
points to Vtable).

If I have 1000 objects of a polymorphic class,
why should i waste 1000*4 bytes for storing the same stuff...

If you know for sure that all contain the same stuff, then you don't
need run-time polymorphism. If you *don't* know and want the compiler
to keep track at runtime ... you have to pay somehow.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top