Hi,
Can anyone clarify me whether C++ compiler creates a VTable for an Abstract Class ?
Thanks
Kiran
Printable View
Hi,
Can anyone clarify me whether C++ compiler creates a VTable for an Abstract Class ?
Thanks
Kiran
Yes.
Most likely so, but it depends on the particular compiler. The vtable is a common implementation technique of virtual function calls, but it isn't required by the C++ language.Quote:
Originally Posted by rsodimbakam
Additionally the compiler may create it, only to have the linker optimize it out of existance.
Thanks for the clarification. This question is something which is confusing my mind a lot.
So the compiler creates the vtable for an abstract class, but since there will be a pure virtual function inside it, compiler does not allow to create an object for Abstract class, as one the vtable entry points to NULL pointer.
Is my analysis correct ?
I'm generalising, somewhat, since the standard doesn't explicitly state this, but what do you think the "= 0" part of a pure virtual definition corresponds to?
I always think of it as putting a zero (i.e. an invalid entry) into the vtable for that function. That's why the class has to be inherited from - so that the derived class can put a valid entry in the appropriate slot. If the derived class doesn't do that, then it is abstract itself. The process continues until all the vtable slots have non-zero entries.
No. The reason a compiler won't allow you to create an object of an abstract class is because C++ stipulates that only concrete classes can be instantiated.Quote:
Originally Posted by rsodimbakam
C++ works the way it does because it's defined to work that way, not because some compiler happens to have a null in some data structure. If all C++ compilers vanished from the face of the earth, C++ still would work the way it does.
I think this is an important distinction. C++ leads and compilers follow, not the other way around.
Nice info.