|
-
September 30th, 2008, 05:54 AM
#1
Does the C++ compiler creates VTable for an Abstract Class
Hi,
Can anyone clarify me whether C++ compiler creates a VTable for an Abstract Class ?
Thanks
Kiran
Last edited by rsodimbakam; September 30th, 2008 at 06:29 AM.
-
September 30th, 2008, 08:42 AM
#2
Re: Does the C++ compiler creates VTable for an Abstract Class
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 30th, 2008, 10:23 AM
#3
Re: Does the C++ compiler creates VTable for an Abstract Class
 Originally Posted by rsodimbakam
Can anyone clarify me whether C++ compiler creates a VTable for an Abstract Class ?
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.
-
September 30th, 2008, 11:59 AM
#4
Re: Does the C++ compiler create VTable for an Abstract Class
Additionally the compiler may create it, only to have the linker optimize it out of existance.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 1st, 2008, 01:46 AM
#5
Re: Does the C++ compiler create VTable for an Abstract Class
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 ?
-
October 1st, 2008, 03:43 AM
#6
Re: Does the C++ compiler create VTable for an Abstract Class
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 1st, 2008, 06:08 AM
#7
Re: Does the C++ compiler create VTable for an Abstract Class
 Originally Posted by rsodimbakam
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 ?
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.
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.
Last edited by _uj; October 1st, 2008 at 01:55 PM.
-
October 1st, 2008, 10:48 PM
#8
Re: Does the C++ compiler create VTable for an Abstract Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|