By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

size of an empty class

P: n/a
Hi,
What will the syntax sound like for checking size of an empty class?
what will be the size.
Thanks,
Cric

Aug 3 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> Hi,
What will the syntax sound like for checking size of an empty class?
what will be the size.
Thanks,
Cric


#include <iostream>

class A
{
};

int main()
{
std::cout << sizeof(A);
return 0;
}

Srini

Aug 3 '05 #2

P: n/a
Ian
fr*******@yahoo.com wrote:
Hi,
What will the syntax sound like for checking size of an empty class?
what will be the size.


At least one byte.

Ian
Aug 3 '05 #3

P: n/a

Ian wrote:
At least one byte.


What is the advantage of using empty class?

-Wg-

Aug 3 '05 #4

P: n/a

WittyGuy schreef:
Ian wrote:
At least one byte.


What is the advantage of using empty class?


Empty classes do not have to take up space when used as base classes.
E.g. an empty class deriving from two empty base classes can also be
1 byte.

The main reason "really" empty classes (i.e. nothing between the { }
and
no base classes ) are in the language is that it would take an extra
rule
to disallow them. That's not worth it

HTH,
Michiel Salters

Aug 3 '05 #5

P: n/a
msalters wrote:
to disallow them. That's not worth it


Do you mean that C++ fails to restrict this? In real-world empty class
is not implemented?

-Wg-

Aug 3 '05 #6

P: n/a
"WittyGuy" <wi**********@gmail.com> writes:
Ian wrote:
At least one byte.


What is the advantage of using empty class?


No advantage. Either you need it & use it, or don't need it & don't
use it.

They are used oft with template metaprogramming or as abstract base
classes.

ImRe
Aug 11 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.