473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

size of an empty class

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
6 4863
> 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
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

Ian wrote:
At least one byte.


What is the advantage of using empty class?

-Wg-

Aug 3 '05 #4

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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: pembed2003 | last post by:
Hi all, If I have the following: class Foo{ void something(void){;} }; and then later I say: Foo f;
18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
1
by: cppfans | last post by:
Why the size of an empty struct or class is 1? i guess 4 before i run this programm. for example: compile with Dev C++ #include <stdio.h> #include <stdlib.h> typedef struct tagEMPTY
8
by: ishekara | last post by:
Hi all, An object of a class A which has no member variables and no member functions, still the size of the object is 1 byte, if there is a byte member variable then also the size of the object...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
3
by: ThazKool | last post by:
Is there anyway to write a class or struct that has no storage. It only operates on a reference to an already existing type. This is actually an extension to another thread. The thread went a...
8
by: ypjofficial | last post by:
Hi all, In what way does the enumerated data type contibute to the size of a class if its part of that class? eg. #include <iostream.h> class one { public:
7
by: Curious | last post by:
Hi, I have created my own data structure. Basically it is a two way 'stack', where I can push elements and pop elements both from top and bottom. I also included a counter to show the number...
2
by: Harish | last post by:
This was an interview question: The size of an empty class is 1 byte. Why? Should it not be 4 bytes? Because an invisible pointer "this" is part of every class?
7
by: Ajay | last post by:
Hi all, Please tell me why empty class has one Byte Size. thanks in Advance.. Ajay
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.