Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > size of an empty class

Reply
Thread Tools

size of an empty class

 
 
free2cric@yahoo.com
Guest
Posts: n/a
 
      08-03-2005
Hi,
What will the syntax sound like for checking size of an empty class?
what will be the size.
Thanks,
Cric

 
Reply With Quote
 
 
 
 
Srini
Guest
Posts: n/a
 
      08-03-2005
> 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

 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      08-03-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
WittyGuy
Guest
Posts: n/a
 
      08-03-2005

Ian wrote:
> At least one byte.


What is the advantage of using empty class?

-Wg-

 
Reply With Quote
 
msalters
Guest
Posts: n/a
 
      08-03-2005

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

 
Reply With Quote
 
WittyGuy
Guest
Posts: n/a
 
      08-03-2005
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-

 
Reply With Quote
 
Imre Palik
Guest
Posts: n/a
 
      08-11-2005
"WittyGuy" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why empty class size is 1 Byte ??? John Carson C++ 10 04-18-2010 11:40 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Size of an empty class why 1 byte ? Sweety C++ 4 08-21-2004 09:46 PM
Re: Why empty class size is 1 Byte ??? raghav C++ 0 07-07-2003 10:44 AM
Re: Why empty class size is 1 Byte ??? Alexander Terekhov C++ 0 07-07-2003 08:47 AM



Advertisments