Thread: Virtual base class constructor

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Virtual base class constructor

    Hello everyone,


    From MSDN virtual base class reference, what means "If a derived class overrides a virtual function that it inherits from a virtual base class, and if a constructor or a destructor for the derived base class calls that function using a pointer to the virtual base class, the compiler may introduce additional hidden "vtordisp" fields into the classes with virtual bases."

    I do not understand why an additional hidden filed is needed?

    http://msdn2.microsoft.com/en-us/lib...DownFilterText


    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You'll have to ask Microsoft that one. This question is specific to the implementation of Microsoft's C++ compilers, and is outside the scope of C++ as a language.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry, grumpy!


    I will post to Windows Programming sub-forum. :-)

    Quote Originally Posted by grumpy View Post
    You'll have to ask Microsoft that one. This question is specific to the implementation of Microsoft's C++ compilers, and is outside the scope of C++ as a language.

    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is not a Windows programming question since it applies generally, but is a Microsoft specific implementation detail.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    I have made a sample below and the output is Derived1. I have monitored that the content of this object in the constructor of class Derived1 is,

    My confusion is, why an additional hidden "vtordisp" fields exists? What is the issue described by the quoted statements in my original question?

    - this 0x0022fc4c {j=-858993460 } Derived1 * const
    - Base {i=100 } Base
    - __vfptr 0x0109783c const Derived1::`vftable' *
    [0] 0x01091037 [thunk]erived1::foo`vtordisp{4294967292,0}' (void) *
    i 100 int
    j -858993460 int


    Code:
    #include <iostream>
    
    using namespace std;
    
    class Base {
    private:
    int i;
    public:
    Base()
    {
    i = 100;
    }
    
    virtual void foo()
    {
    cout << "Base " << endl;
    }
    };
    
    class Derived1 : virtual public Base {
    private:
    int j;
    public:
    Derived1()
    {
    Base* pb = this;
    pb->foo(); // call Base class's virtual function inside derived class's
    constructor by using pointed to base type
    j = 200;
    }
    
    virtual void foo()
    {
    cout << "Derived1 " << endl;
    }
    };
    
    class Derived2 : virtual public Base {
    private:
    int k;
    public:
    Derived2()
    {
    k = 300;
    }
    
    virtual void foo()
    {
    cout << "Derived2 " << endl;
    }
    };
    
    class Final : public Derived1, public Derived2 {
    private:
    int t;
    public:
    Final()
    {
    t = 400;
    }
    
    virtual void foo()
    {
    cout << "Final " << endl;
    }
    };
    
    int main()
    {
    Final f;
    
    return 0;
    }
    Quote Originally Posted by laserlight View Post
    It is not a Windows programming question since it applies generally, but is a Microsoft specific implementation detail.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Virtual Base Classes
    By skewray in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2006, 06:56 PM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM