Thread: Why Virtual base class increase the size of class by 4 bytes?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    40

    Why Virtual base class increase the size of class by 4 bytes?

    Hi all,

    Well I have a quick doubt on virtual base class:
    can sombody explain?
    I am going to write 2 c++ program please check output.

    Code:
    #include<iostream>
    using namespace std;
    
    class base
    {
    	int i;
    };
    
    class derived: public base
    {
    };
    
    
    void main()
    {
    	cout<<"size of class derived is "<<sizeof(derived)<<endl;
    }
    
    
    OUTPUT:
    size of class derived is 4
    Press any key to continue
    
    
    With virtual:
    
    #include<iostream>
    using namespace std;
    
    class base
    {
    	int i;
    };
    
    class derived:virtual public base
    {
    };
    
    
    void main()
    {
    	cout<<"size of class derived is "<<sizeof(derived)<<endl;
    }
    
    OUTPUT:
    size of class derived is 8
    Press any key to continue

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Puget Sound, WA
    Posts
    3
    Well one don't use void main(), it's int and must return zero on success.

    What do you want to know about virtual classes or what is your doubt?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Quote Originally Posted by MosaicFuneral View Post
    Well one don't use void main(), it's int and must return zero on success.

    What do you want to know about virtual classes or what is your doubt?
    Did you notice, I have written 2 programs.
    output of1st is 4 byte and output of second is 8 byte(uisng virtual base class). Now my doubt is, mentioned in title. i.e. "will Virtual base class increase the size of a class".

    Thanks for telling me about usage of int main instead of void main.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Puget Sound, WA
    Posts
    3
    I just skim thread titles nowadays; since most of them on a good percentage of sites involve the words "please, give, me" in all caps.

    Is your compiler just treating based:derived in the first example as the same [empty] class?
    Last edited by MosaicFuneral; 07-08-2009 at 01:25 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As soon as you add the keyword virtual to your class, it's going to go up by the size of a pointer (on most compilers) because that's what the class needs added to it to implement the feature.
    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"

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Quote Originally Posted by iMalc View Post
    As soon as you add the keyword virtual to your class, it's going to go up by the size of a pointer (on most compilers) because that's what the class needs added to it to implement the feature.
    I could not understand, please explain in simple way.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This link explains virtual inheritance very well, including where the extra 4 bytes comes from.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vaibhavs17 View Post
    Now my doubt is, mentioned in title. i.e. "will Virtual base class increase the size of a class".
    BTW, that is not a doubt, it's a question.
    A doubt means you don't believe something is true.
    A question is when you are asking for an explanation about something that you're not sure of, and it usually ends with a question mark "?".
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> BTW, that is not a doubt, it's a question.

    Feeling a bit pedantistic today, are we?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sebastiani View Post
    >> BTW, that is not a doubt, it's a question.

    Feeling a bit pedantistic today, are we?
    ... must ... resist ... changing ... to ... "pedantic" ... grrrr ....

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ... must ... resist ... changing ... to ... "pedantic" ... grrrr ....

    Oh, the irony. Just to be sure, though (and yes I realize that I pretty much made that word up), I believe that is a legal (if not fugly) adjectivication of the word. Then again, English never was my best subject.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Sebastiani View Post
    >> BTW, that is not a doubt, it's a question.

    Feeling a bit pedantistic today, are we?
    Someone really has to find the dictionary that has that incorrect translation and burn it (and all copies of it).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Someone really has to find the dictionary that has that incorrect translation and burn it (and all copies of it).

    I remember my sister forcing me to read the entire dictionary due to my poor vocabulary. It actually helped quite a lot, but I'm still prone to make words up now and then. It may have something to do with the fact that I tend to break words up logically into roots and modifiers (not always correctly, either), so I often end up with words like 'pedantistic' or such.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Sebastiani View Post
    >> Someone really has to find the dictionary that has that incorrect translation and burn it (and all copies of it).

    I remember my sister forcing me to read the entire dictionary due to my poor vocabulary. It actually helped quite a lot, but I'm still prone to make words up now and then. It may have something to do with the fact that I tend to break words up logically into roots and modifiers (not always correctly, either), so I often end up with words like 'pedantistic' or such.
    Or "adjectivication" .

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by iMalc View Post
    Or "adjectivication" .
    and don't forget edumacation.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 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

Tags for this Thread