473,796 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size of a class having enumerated data types?

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:
enum months{jan =1,feb =2};//till dec = 12
};

int main()
{
cout<<sizeof(on e);//outputs 1
cout<<sizeof(on e::months);//outputs 4
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?

Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #1
8 2201
<yp*********@in diatimes.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com
Hi all,

In what way does the enumerated data type contibute to the size of a
class if its part of that class?
The mere definition of a data type never contributes to the size of a class.
eg.
#include <iostream.h>
class one
{
public:
enum months{jan =1,feb =2};//till dec = 12
};

int main()
{
cout<<sizeof(on e);//outputs 1
cout<<sizeof(on e::months);//outputs 4
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?


Consider:

#include <iostream>
using namespace std;

class two
{
public:
class inner
{
int x;
};
};

int main()
{
cout << sizeof(two) << endl; //outputs 1
cout << sizeof(two::inn er) << endl; // outputs 4

return 0;
}

If you want a contribution to the size of the class, then you need to
declare an object of the data type that you have defined, e.g.,

class one
{
public:
enum months{jan =1,feb =2};
months object;
};
class two
{
public:
class inner
{
int x;
};
inner object;
};
int main()
{
cout << sizeof(one) << endl; //outputs 4
cout << sizeof(two) << endl; //outputs 4

return 0;
}
--
John Carson
Jan 23 '06 #2
<yp*********@in diatimes.com> schrieb im Newsbeitrag
news:11******** *************@g 47g2000cwa.goog legroups.com...
....
class one
{
public:
enum months{jan =1,feb =2};//till dec = 12
};
int main()
{
cout<<sizeof(on e);//outputs 1
This prints the size of an instance of class one. Since that class does not
does not define any data members, the size of an object of that type is 1.
cout<<sizeof(on e::months);//outputs 4
This prints the size of an instance of one::months. And since that is an
enumaration type, the size of its members is, IIRC, unspecified.
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?


A type has no size. Only objects have a size, and a type is not an object.
Yes, you can write sizeof(type), but that does return the size of an object
of the specified type.

HTH
Heinz

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #3
In article <11************ *********@g47g2 000cwa.googlegr oups.com>,
yp*********@ind iatimes.com writes
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> Warning: pre-standard headerclass one
{
public:
enum months{jan =1,feb =2};//till dec = 12 No need to assign values after the first one. However this is just a
definition of a user defined type and so has zero size. Instances of the
enum will have the size of whatever integer type the implementation uses
(could be as small as a char in this case, but must nott be larger than
an int)};

int main()
{
cout<<sizeof(o ne);//outputs 1 As one has no data members it has the minimum allowed size for any type
of 1.
cout<<sizeof(o ne::months);//outputs 4
Which tells you how much storage is need for a variable of type
one::months
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?

Because it isn't data member of the class (nor do functions contribute
to the size of the class.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #4
Hello Yogesh,

In your class enum is declared as one type. And it is not instantiated.
On instantiating the object of this class there will be no member
variable of type enum months in the object. Therefore the sizeof this
class will only show the size of the empty class.

But if you create a member variable of type enum months in your class
then it will include the enums size in your class size....

class one
{
public:
enum months{jan =1,feb =2} m_variable; //till dec = 12

};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #5

<yp*********@in diatimes.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
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:
enum months{jan =1,feb =2};//till dec = 12
};

int main()
{
cout<<sizeof(on e);//outputs 1
cout<<sizeof(on e::months);//outputs 4
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?

Thanks and Regards,
Yogesh Joshi


Because you only declared the enum months, you never instantized it.

change it to:
class one
{
public:
enum months { jan = 1, feb = 2 } mymonths;
};

or
enum months { jan = 1, feb = 2 };
months mymonths;

Now do a sizeof(one) and it'll be 4.

enum months { jan }; only declares months, it doesn't create a variable
called months.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #6
You didn't actually declare a variable/instance of months type. All you
did was declare an enumerated type called months. In fact you can
typedef or declare a nested class/struct inside your class and as long
as you don't declare any varible of of that type, the size of the class
won't be affected. Your class 'one' is empty since you did not declare
any variable. The size of any empty class is implementation depandant
but always greater than zero.

The size of any enum is also implementation dependant but usually
either the size of a byte or an integer. It also depends on the largest
size of an enumerated value.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #7
yp*********@ind iatimes.com wrote:
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:
enum months{jan =1,feb =2};//till dec = 12
};

int main()
{
cout<<sizeof(on e);//outputs 1
cout<<sizeof(on e::months);//outputs 4
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?


Because it is - as you wrote above - a type. Types themselves don't need any
memory (as far as the C++ standard is concerned). Only instances of them
do, but there is no instance of your enum.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #8
yp*********@ind iatimes.com wrote:
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:
enum months{jan =1,feb =2};//till dec = 12
};

int main()
{
cout<<sizeof(on e);//outputs 1
cout<<sizeof(on e::months);//outputs 4
}

so why the size of enumerated datatype months doesn't get contributed
to the size of the one class?


That's because sizeof(one) calculates the size of an object of type one.
one::months is a type not a data member therefore it does not grow one's
size.

To give an analogue, consider an equivalent:

class one{};

enum months {/*...*/};

int main(){
one o;
cout << sizeof(a); // 1
}

The only difference is that now you refer one::month as just month.

In case you are curious why an empty one class has a size (of 1), this
is because it needs to take up a bit of memory so its memory address is
unique.

Ben
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Jan 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2522
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to select a type on the basis of its traits. More specifically, I would like to select an unsigned integer type on the basis of the number of bits I need it to have. (This is because there are particularly efficient ways of implementing the Advanced
19
4921
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
7
2629
by: John Goche | last post by:
Hello, The following program compiler and runs fine under gcc and produces the output 3. However, I am not sure whether such behavior is legal. In particular, as a related question, how would I go about checking that a supplied integer is contained within the set of values of an enumeration? Is there a way to do this? Thanks,
70
3382
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static void MessageBeep(BeepTypes type){ if(!MessageBeep((UInt32) type)){ Int32 err = Marshal.GetLastWin32Error();
1
1615
by: senfo | last post by:
I'm using an enumerated type to identify a record type that relates to a unique ID in a database. Identity columns in SQL start at 1, while enumerated types in C# start at 0. I didn't think it would be a big deal because I could just force the values of the enum to start at 1. Database: ID RecordType -- ---------- 1 TypeA
1
2938
by: =?Utf-8?B?R292ZXJubWVudE1hbg==?= | last post by:
Hi, I am using Reflection to build a GUI based on a class create with XSD, in VB ..NET 2.0. For most elements, I can easily determine the Type and then provide the user some controls to select what data to use (they are creating an XML document essentially on the fly). But the choice element has several possible types and so it creates this itemField as object within the xs:choice-generated class: Private itemField As Object
20
4045
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
36
3820
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry animal for sacrifice to a big armour plated one... but will offer it anyway. Please do suggest...
44
2960
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the class is used as a function that keeps state from one call to the next. The problem is that I don't know what to call such a thing! "Abstract class" isn't right, because that implies that you should subclass the class and then instantiate the...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.