C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 12:35 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 35
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
vaibhavs17 is offline   Reply With Quote
Old 07-08-2009, 12:51 PM   #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?
MosaicFuneral is offline   Reply With Quote
Old 07-08-2009, 12:59 PM   #3
Registered User
 
Join Date: Mar 2009
Posts: 35
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.
vaibhavs17 is offline   Reply With Quote
Old 07-08-2009, 01:22 PM   #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.
MosaicFuneral is offline   Reply With Quote
Old 07-08-2009, 01:30 PM   #5
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,476
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
iMalc is offline   Reply With Quote
Old 07-08-2009, 01:32 PM   #6
Registered User
 
Join Date: Mar 2009
Posts: 35
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.
vaibhavs17 is offline   Reply With Quote
Old 07-08-2009, 01:47 PM   #7
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
This link explains virtual inheritance very well, including where the extra 4 bytes comes from.
bithub is offline   Reply With Quote
Old 07-08-2009, 02:13 PM   #8
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
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
cpjust is offline   Reply With Quote
Old 07-08-2009, 03:24 PM   #9
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> BTW, that is not a doubt, it's a question.

Feeling a bit pedantistic today, are we?
Sebastiani is offline   Reply With Quote
Old 07-08-2009, 03:27 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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 ....
tabstop is offline   Reply With Quote
Old 07-08-2009, 03:43 PM   #11
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> ... 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.
Sebastiani is offline   Reply With Quote
Old 07-08-2009, 08:09 PM   #12
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
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
cpjust is offline   Reply With Quote
Old 07-08-2009, 08:29 PM   #13
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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.
Sebastiani is offline   Reply With Quote
Old 07-09-2009, 12:56 AM   #14
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,476
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" .
iMalc is offline   Reply With Quote
Old 07-09-2009, 06:55 AM   #15
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
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
cpjust is offline   Reply With Quote
Reply

Tags
code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it required to provide default definitions for virtual functions in a base class? Canadian0469 C++ Programming 7 11-17-2008 01:00 PM
virtual base class constructor George2 Windows Programming 1 03-24-2008 12:43 AM
Error with a vector Tropicalia C++ Programming 20 09-28-2006 07:45 PM
C++ XML Class edwardtisdale C++ Programming 0 12-10-2001 11:14 PM
Exporting Object Hierarchies from a DLL andy668 C++ Programming 0 10-20-2001 01:26 PM


All times are GMT -6. The time now is 08:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22