Thread: virtual inhertance *intersting*

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    virtual inhertance *intersting*

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    class sample
    {
    	int i;
    	virtual void fun(){};
    };
    class Derived_s: public  virtual sample
    {
    };
    
    class Derived_s2: public  virtual sample
    {
    };
    
    class Derived_m: public  Derived_s, Derived_s2
    {
    };
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Derived_s ds;              
    	Derived_s2 ds2;
    	Derived_m dm;
    	cout<<sizeof(ds)<<"  byte\n";  
    	cout<<sizeof(ds2)<<"  byte\n";
    	cout<<sizeof(dm)<<"  byte\n"; //20 bye when no virtual decalred in deriving ds
                                                               //or else 16 byte
    	return 0;
    }
    result:
    12 byte fine. 4(int)+ 4 (vptr)+ 4 vptr for inheritence ....fine
    12 byte . same as above
    16 byte . 4 (int) + 4 (vptr for function) + 2 x 4 (for each virtual inheritance vtpr)
    Pls, Correct me if wrong.


    Then , if remove virtual keyword from any of the derivation of Derived_s or Derived_s2 and make it like

    Code:
    class Derived_s: public  sample
    {
    };
    
    class Derived_s2: public  virtual sample
    {
    };
    the size of dm becomes 20. why?

    or, also changing the above code like this,

    class Derived_m: public virtual Derived_s, virtual Derived_s2
    {
    };


    makes the size of dm as 20 bytes. why?

    Pls, help me understand the issue claerly.

    Warm Reagrds,
    Alex

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    the size of dm becomes 20. why?
    Because now you have 2 instances of the "sample" class. That means Derived_s::i and Derived_s2::i are 2 different variables. Using virtual inheritance makes it so there is only 1 instance of the "sample" class, so there is only 1 "i" variable.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Alexpo View Post
    [CODE]
    the size of dm becomes 20. why?
    Because that's what the particular compiler chose in this instance. Change anything (code, compiler, or compiler settings) and the result could be entirely different. For example, there is perhaps nothing's stopping a smart compiler from optimising out the "i" member. Then it could apply EBO...

    Honestly, you souldn't be fixated with the actual size. It really isn't your concern. It's more important to simply understand how virtual inheritance works, and you probably wont get that from just looking at the sizes of stuff. It'll probably only confuse you furthur.
    Learn the 'how' and you might find out the 'why' along the way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM