CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2002
    Location
    Pune
    Posts
    61

    Virtual Base Class

    How is the size of an object (of the class created by using Hybrid Inheritance) calculated ?

    When I saw the sizeof the object involving Virtual Base Classes, it was different then what I expected. What exactly is the logic ?

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Post an example of what you think is different. Also,what is "Hybrid Inheritance?"

    Kuphryn

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Hybrid inheritance == multiple inheritance (presumably).
    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


  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Virtual Base Class

    Originally posted by Nowwin
    When I saw the sizeof the object involving Virtual Base Classes, it was different then what I expected. What exactly is the logic ?
    Depends on the compiler. One compiler can implement virtual base classes completely different from another compiler.

    Your best bet into trying to figure it out is to actually get the specs for the compiler and its VBC implementation. I believe that some compiler vendors supply this information.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    May b the OP was'nt expecting Vptr to be included in the size of the Class?????????
    Last edited by usman999_1; March 25th, 2003 at 05:45 AM.

  6. #6
    Join Date
    Oct 2002
    Location
    Pune
    Posts
    61
    To clarify the question I am attaching following code:


    Code:
    class A
    {
    public :
    	int a;
    	int b;
    } ;
    
    
    class B : public virtual A
    //class B : public A
    {
    public :
    	int a;
    } ;
    
    
    //class C : public virtual A
    class C : public A
    {
    public :
    	int a;
    } ;
    
    
    class HYBRID : public B,C
    {
    public :
    	int a;
    } ;
    
    
    void main ()
    {
    	HYBRID  obj ;
    	cout << sizeof (obj) ;
    }



    Uncomment the lines and see the changing results.

    Hope u got it.

  7. #7
    Join Date
    Mar 2003
    Location
    China
    Posts
    10
    Originally posted by Nowwin
    To clarify the question I am attaching following code:


    Code:
    class A
    {
    public :
    	int a;
    	int b;
    } ;
    
    
    class B : public virtual A
    //class B : public A
    {
    public :
    	int a;
    } ;
    
    
    //class C : public virtual A
    class C : public A
    {
    public :
    	int a;
    } ;
    
    
    class HYBRID : public B,C
    {
    public :
    	int a;
    } ;
    
    
    void main ()
    {
    	HYBRID  obj ;
    	cout << sizeof (obj) ;
    }



    Uncomment the lines and see the changing results.

    Hope u got it.
    So, what's the difference between appling the virtual or not ?
    Speed up! Speed up! Speed up!

    It can be faster!!!

  8. #8
    Join Date
    Oct 2002
    Location
    Pune
    Posts
    61

    Virtual Base Class

    I want you to notice the difference in the size of the object (by uncommenting the commented line, and commenting the uncommented line).


    i.e First keep the code as it is and see the output. Then uncomment the commented lines and comment the uncommented lines, for e.g.

    original code :

    class B : public virtual A
    //class B : public A
    {
    ....
    } ;



    new code:

    //class B : public virtual A
    class B : public A
    {
    ....
    } ;

    The output differs.

    You can try out these combinations:
    1. B,C both deriving from virtual base class.
    2. Only B deriving from virtual base class.
    3. Only C deriving from virtual base class.
    4. No class deriving from virtual base class.



    There is change in output. Why the change? What is the logic?
    Is there any extra field (like vptr in case of virtual functions) kept by the compiler ?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Virtual Base Class

    Originally posted by Nowwin
    There is change in output. Why the change? What is the logic?
    Is there any extra field (like vptr in case of virtual functions) kept by the compiler ?
    Maybe there are 1, 2, 3, or a whole set of information kept by the compiler. Like I stated, no one, unless they have documentation from Microsoft, can be 100% sure how this is implemented. I believe there are also implementations of VBC's where the data is not stored in contiguous memory, so doing a memcpy on an instance of a VBC will screw up things totally.

    Forgive me for asking, but why is it important for you to know what the sizeof a derived virtual base class is?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Sep 2002
    Posts
    1,747
    These classes will have no vtable or other internal implementation of virtualization, as there are no virtual methods. Instead, they use virtual inheritence, which can be thought of as the "singleton of inheritence". If a class is not declared as being virtually inherited, diamond inheritence will cause multiple copies of the state members of the mutual base in the derived, which can be resolved through scoping through the intermediate discriminating classes. Adding virtual relieves the necessity for disambiguation between paths as only one copy of the mutually inherited class's state members are stored in the derived.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  11. #11
    Join Date
    Sep 2002
    Posts
    1,747
    Oh yeah.

    And virtual, like static, is a highly overloaded word in c++, which can often be confusing.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  12. #12
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Have a look at this code:
    Code:
    class A 
    {
    public:
        A(int i):i_(i){}
        int i_;
    };
    
    class B: public A
    {
    public:
        B(int i):A(i){}
    };
    
    class C: public A
    {
    public:
        C(int i):A(i){}
    };
    
    class vB: public virtual A
    {
    public:
        vB(int i):A(i){}
    };
    
    class vC: public virtual A
    {
    public:
        vC(int i):A(i){}
    };
    
    class D: public B, public C
    {
    public:
        class D(int i):B(i), C(i){}
    };
    
    class E: public vB, public vC
    {
    public:
        E(int i):A(i), vB(i), vC(i){}
    };
    
    int main(int argc, char* argv[])
    {
        D d(0);
        d.B::i_ = 0;
        d.C::i_ = 1;
        // d.i_ = ???   <--- error i_ is ambiguous
        E e(0);
        e.i_ = 0;
        cout<<sizeof(D)<<endl;
        cout<<sizeof(E)<<endl;
        return 0;
    }
    D has two base classes of type A (indirect, one thru B and one thru C) while E has only one base class of type A because of the virtual inheritance. This, and the presence of a vtable (compiler dependent) will make out the difference of size between D and E.

    Also note the line I commented out. Remove the comment and replace "???" by a numerical value and see what the compiler sais.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  13. #13
    Join Date
    Oct 2002
    Location
    Pune
    Posts
    61

    Virtual Base Class

    Thank You.

    But still, can anybody guess as to how the virtual base classes are implemented? Atleast a way in which it can be implemented.


    Thanks once again for such prompt replies. It is turning out to be a nice discussion.

  14. #14
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Virtual Base Class

    Originally posted by Nowwin
    But still, can anybody guess as to how the virtual base classes are implemented? Atleast a way in which it can be implemented.
    You shouldn't care about that if you don't want to write a C++ compiler. Even if you knew how a specific compiler implements virtual base classes, using this knowledge in your code would be a non-portable hack.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

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