CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    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.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Does the C++ compiler creates VTable for an Abstract Class

    Yes.
    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


  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Does the C++ compiler creates VTable for an Abstract Class

    Quote 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.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  5. #5
    Join Date
    Sep 2008
    Posts
    48

    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 ?

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Does the C++ compiler create VTable for an Abstract Class

    Quote 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.

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Does the C++ compiler create VTable for an Abstract Class

    Nice info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured