Virtual Function mechanism in c++


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Virtual Function mechanism in c++

  1. #1
    Join Date
    Oct 2005
    Posts
    22

    Question Virtual Function mechanism in c++

    I have some pieces of texts from articles on virtual functions...

    Inside an ordinary member function you can imagine what will happen if you call a virtual function – the virtual call is resolved at runtime because the object cannot know whether it belongs to the class the member function is in, or some class derived from it.

    also..

    Once the VPTR is initialized to the proper VTABLE, the object in effect "knows" what type it is.

    also..

    When a constructor is called, one of the first things it does is initialize its VPTR. However, it can only know that it is of the “current” type – the type the constructor was written for. The constructor code is completely ignorant of whether or not the object is in the base of another class.

    in all these texts, i dnt understand what the author means by "the object knows"??, what and how it can know? what does the word knows refer to???

    also, so is it true that when we make an object and then the constructors are called from most base to current class in inheritance chain, then vptr also gets initialized in each constructor to vtable corresponding to that class in the inheritance chain till constructor of the current class is called finally in the constructor calls chain and it initializes the final state of vptr to vtable of the current class???? :confused:

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    What is meant by 'know' is 'the compiler can determine that the object is of this type or has these members or whatnot'. Know implies a living being -- using it in this way is one of those messed up "common usage" things found in every language.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    "knows" refers to the type information that the object being initialized has access to. If you use some for of type detection inside the constructor, say typeid(*this), you will get the this type. Another way to determine what the object knows about its owen type is calling a virtual function and examining which function is called: the base class's or the derived's.

    When there is an inheritance chain, each constructor performs its own initialization, because the same constructor is used to initialize an object of thus type, regardless of whether the object being initialized is a derived object or a base class object. So by the time the base class ctor has finished executing but the derived ctor hasn't yet started, the object has access to the VTABLE of the base class, not the derived's. Only after the most derived class's constructor has finished executing (successfully, that is), is the object considered fully constructed and its liftime begins.
    Last edited by Danny; 10-21-2005 at 11:23 AM.
    Danny Kalev

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  2. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 12:18 PM
  3. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 05:21 PM
  4. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center