![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 35
| Why Virtual base class increase the size of class by 4 bytes? 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 | |
| | #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 | |
| | #3 | |
| Registered User Join Date: Mar 2009
Posts: 35
| Quote:
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 | |
| | #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 | |
| | #5 |
| Algorithm Dissector 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 | |
| | #6 |
| Registered User Join Date: Mar 2009
Posts: 35
| |
| vaibhavs17 is offline | |
| | #8 | |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| Quote:
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 | |
| | #9 |
| Guest 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 | |
| | #10 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #11 |
| Guest 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 | |
| | #12 | |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| Quote:
__________________ "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 | |
| | #13 |
| Guest 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 | |
| | #14 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| Quote:
. | |
| iMalc is offline | |
| | #15 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
|
__________________ "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 | |
![]() |
| Tags |
| code |
| Thread Tools | |
| Display Modes | |
|
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 |