DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Virtual Inheritance

  1. #1
    Join Date
    Dec 2003
    Location
    India
    Posts
    61

    Virtual Inheritance

    Hi,

    I'm trying to understand Virtual Inheritance by reading about Virtual base classes.

    My understaning as of now is that Virtual inheritance is useful only when a Class derives from mulitiple parents which have a common parent.In this case only a sigle copy of the grand parent object is stored within the most derived object, when it is instantiated.

    I tried a sample program to see the size of classes which implement virtual inheritance.The code is here...

    Code:
    class Animal
    {
    public:
    	int characteristics[100];
    	void Sleep()
    	{
    		cout << "Sleeping" <<"\n";
    	}
    };
    
    class Dog : public virtual Animal
    {
    };
    
    class Bird : public virtual Animal
    {
    };
    
    class DogBird : public Dog,public Bird
    {
    };
    
    
    void main(int argc, char** argv)
    {
    	cout << "sizeof Animal is " << sizeof(Animal) << "\n";
    	cout << "sizeof Dog is " << sizeof(Dog) << "\n";
    	cout << "sizeof Bird is " << sizeof(Bird) << "\n";
    	cout << "sizeof DogBird is " << sizeof(DogBird) << "\n";
    	
    	DogBird db;
    	db.Sleep(); 
    }
    
    results...
    
    sizeof Animal is 400
    sizeof Dog is 404
    sizeof Bird is 404
    sizeof DogBird is 408
    Sleeping
    The results were surprising and i dont understand why.I understand the Sizeof Animal. But why the Sizeof Dog and Bird is 404 bytes? I believe they will receive their individual copy of Animal object to be stored within them and expected its size also be of 400 bytes.And the DogBird object does get its own (only)copy of Animal object and also should have the size of 400.
    bytes.

    What is happening here?

    Thanks,
    Suresh.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    First of all, the implementation details of VI are compiler dependent, and in many cases, you can change the compiler's setting with respect to VI.

    Your understanding of VI usage is correct. However, in some rare cases, one uses VI also when the derived class has a single base class, i.e., without using multiple inheritance. The reasone for using VI in this particular case is to ensure that the virtual base is constructed before any other base/embedded object (the construction orde is roughly this: virual bases, then non-virtual bases and embedded object, finally the most derived object).

    As for the results you got: First, you want to declare at least one virtual function in Animal if this class is used as a base class for other classes. This is how base classes in general are used, and it will also change the results of your program.
    Back to your question: Dog and Bird have an implicit data member that the compiler adds to classes that have virtual base classes. This member is a pointer pointing to the Animal subobject. In the case of single inheritance, the compiler doesn't need this pointer because there is only one copy of the base but in VI compilers use pointers to access the virtual base. This way, they ensure that only a single instance of the virtual base is kept in the inheritance lattice. So what happens is that instead of having multiple instances of the virtual base subobject in the grandchildren objects, you get a single shared instance of the virtual base, but multiple pointers to that virtual base so that every subobject can access it directly. However, as I said, they results are very much compiler dependent (and alignment dependent) so you could easily find compilers that use a different size for Dog and Bird.
    Last edited by Danny; 11-13-2006 at 07:24 AM.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    Location
    India
    Posts
    61
    "This way, they ensure that only a single instance of the virtual base is kept in the inheritance lattice"

    I agree on this.There should be only one copy of Animal object in the Inheritance lattice.For this to happen all the subobjects (Dog,Bird,DogBird) needs a pointer which points to the single Animal Object.Assuming the size of a Pointer is 4 bytes in my machine all the subobjects should have the size of 4 bytes approximately,which is enough to point a Animal object(and these subobjects doesnt declare any members on their own) .

    Why is that 400 bytes extra for Dog and Bird?Do they have their own copies of Animal Object and a pointer to the common animal object?

    And 404 bytes extra for Dogbird.I'm clueless about this.

    Thanks,
    Suresh.
    Last edited by sureshsundar007; 11-14-2006 at 02:53 AM.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Every object that is derived from Animal must have a single copy of Animal. And where is the best place to store that single copy? Inside the derived object itself, of couse! So both Bird & Dog have to have their copy of Animal. Notice that DogBird without VI would have two copies of Animal, not one, so its size would be >800.
    The shared virtual base class isn't shared among all derived classes or among all objects; rather, the VI mechanism ensures that each derived object has a single copy of the virtual subobject. In other words, it's not similar to the case of static data members of a class, where a single object is shared by all objects.
    Danny Kalev

Similar Threads

  1. Replies: 4
    Last Post: 10-27-2005, 11:00 PM
  2. Virtual Function mechanism in c++
    By premartha in forum C++
    Replies: 2
    Last Post: 10-21-2005, 10:11 AM
  3. non virtual destructor is called
    By drkybelk in forum C++
    Replies: 3
    Last Post: 03-17-2005, 09:27 AM
  4. files
    By langevin in forum .NET
    Replies: 1
    Last Post: 09-13-2002, 08:57 PM
  5. WebFroms and Virtual Directories
    By Madhu in forum ASP.NET
    Replies: 2
    Last Post: 05-08-2002, 11:36 AM

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