473,396 Members | 2,011 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,396 software developers and data experts.

static enumeration alternative

I would like to write, within the private section of my class
definition:

static enum TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT};

but C++ does not allow this. The four values above are constant and
static, and will be used only by a private class method.

I could just do:

static const int TRUNC_TOP;
static const int TRUNC_RIGHT;
etc...

initializing them to distinct values in my .cpp file.

But it would be nicer to use an enum, or somehting else specifically
suited to this purpose. What is the best way to what I'm trying to
do? Ie, declare four static class constants whose values are
irrelevant except as distinguishing markers.

Thanks,
cpp
Jul 22 '05 #1
10 1785
"cppaddict" <he***@hello.com> wrote...
I would like to write, within the private section of my class
definition:

static enum TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT};

but C++ does not allow this. The four values above are constant and
static, and will be used only by a private class method.
If you drop 'static', then it should do what you need. If you place
that enumeration declaration in the private section, it should be
inaccessible to anybody but the class itself and its friends.

I could just do:

static const int TRUNC_TOP;
static const int TRUNC_RIGHT;
etc...

initializing them to distinct values in my .cpp file.

But it would be nicer to use an enum, or somehting else specifically
suited to this purpose. What is the best way to what I'm trying to
do? Ie, declare four static class constants whose values are
irrelevant except as distinguishing markers.


You're probably confusing enumerators and static data. Enumerators
are not static data. They are named constants.

Victor
Jul 22 '05 #2
You're probably confusing enumerators and static data. Enumerators
are not static data. They are named constants.


Right. But in this case I would like my named constants to be static.
There's no reason for each instance to have its own copy of them.
Other than that, an enumeration is perfect. So the decision is
between:

1. Use static named constants (bulkier syntax, but memory savings)

2. Use enum (cleaner syntax, but unnecessary duplicates)

What do you think?

Thanks,
cpp

Jul 22 '05 #3
cppaddict wrote:
You're probably confusing enumerators and static data. Enumerators
are not static data. They are named constants.


Right. But in this case I would like my named constants to be static.
There's no reason for each instance to have its own copy of them.
Other than that, an enumeration is perfect. So the decision is
between:

1. Use static named constants (bulkier syntax, but memory savings)

2. Use enum (cleaner syntax, but unnecessary duplicates)


My understanding is that enum declarations declare *types* not *variables*.
No memory should be unsed for a type declaration.
Best

Kai-Uwe Bux
Jul 22 '05 #4
My understanding is that enum declarations declare *types* not *variables*.
No memory should be unsed for a type declaration.


But when an instance object is created, memory is allocated for each
of the TruncTypes. I think this must be the case, since enums can be
cast to int. In my example, there would be memory allocated for the
int's 0,1,2,3 for every object created. When in fact I only that
memory to be allocated once, since it can be shared among all the
objects.

I could be missing something, not sure....

cpp
Jul 22 '05 #5
"cppaddict" <he***@hello.com> wrote...
My understanding is that enum declarations declare *types* not *variables*.No memory should be unsed for a type declaration.
But when an instance object is created, memory is allocated for each
of the TruncTypes.


Who told you that? That's not true.
I think this must be the case, since enums can be
cast to int. In my example, there would be memory allocated for the
int's 0,1,2,3 for every object created. When in fact I only that
memory to be allocated once, since it can be shared among all the
objects.
I cant say I that sentence.

I could be missing something, not sure....


You're missing a whole lot, AFAICT.
Jul 22 '05 #6
cppaddict wrote:
My understanding is that enum declarations declare *types* not
*variables*. No memory should be unsed for a type declaration.
But when an instance object is created, memory is allocated for each
of the TruncTypes.


Nope, memory is only allocated for the data fields of the object. The
declaration

enum TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT}

tells the compiler to treat TRUNC_TOP as a name for some constant of type
TruncType, however this constant does not reside in memory. E.g., I think
you cannot take the address &TRUNC_TOP. Same as there is nothing like the
address of a specific int: &314.
I think this must be the case, since enums can be
cast to int.
That you can cast a value to int does not imply that value has to have an
address in memory.
In my example, there would be memory allocated for the
int's 0,1,2,3 for every object created.


Why would the compiler do that? The values 0,1,2,3 are known at compile
time. There is no need to store them at all. Your program certainly can use
the type unsigned long without some part of memory being used to store all
the values 0,1,2,3,4,5,6,7,8,...

Maybe it is most convincing, if you just try it out:

class X {
private:

enum whatever {
// make this as long as you can:
a,b,c,d,e,f,g,h,j,k,i,u,z,t,r,w
};

public:

whatever W;

};

#include <iostream>

int main ( void ) {
X x;
std::cout << sizeof( X ) << " " << sizeof( x ) << std::endl;
}

You will find that the only memory actually allocated is for the field W.
Best

Kai-Uwe Bux
Jul 22 '05 #7
>> But when an instance object is created, memory is allocated for each
of the TruncTypes.
Who told you that? That's not true.


It's based on the following reasoning; let me know where you think
it's flawed. Ater I create my object, my methods can refer to things
like TRUNC_TOP and TRUNC_BOTTOM, can they not? And they could cast
those values to an int, too. If there is no memory allocated for
them, where is that int coming from?
I think this must be the case, since enums can be
cast to int. In my example, there would be memory allocated for the
int's 0,1,2,3 for every object created. When in fact I only that
memory to be allocated once, since it can be shared among all the
objects.


I cant say I that sentence.


The above should have read: "When in fact I only need that memory to
be allocated once." Sorry about that...

cpp

Jul 22 '05 #8
Maybe it is most convincing, if you just try it out:

class X {
private:

enum whatever {
// make this as long as you can:
a,b,c,d,e,f,g,h,j,k,i,u,z,t,r,w
};

public:

whatever W;

};

#include <iostream>

int main ( void ) {
X x;
std::cout << sizeof( X ) << " " << sizeof( x ) << std::endl;
}

You will find that the only memory actually allocated is for the field W.
Best

Kai-Uwe Bux


Kai-Uwe,

Thanks for that explanation. That cleared up a serious
minsunderstanding of mine. So I guess my reservations about using an
enum here were unfounded. I'm just going to use it...

Thanks again,
cpp

Jul 22 '05 #9

"cppaddict" <he***@hello.com> wrote in message
news:0f********************************@4ax.com...
But when an instance object is created, memory is allocated for each
of the TruncTypes.


Who told you that? That's not true.


It's based on the following reasoning; let me know where you think
it's flawed. Ater I create my object, my methods can refer to things
like TRUNC_TOP and TRUNC_BOTTOM, can they not? And they could cast
those values to an int, too. If there is no memory allocated for
them, where is that int coming from?


It goes directly into the machine code generated.

int x = TRUNC_TOP;

Machine code (this is made up machine code)

mov sp[12], #1 ; move integer 1 into space reserved for variable x

You might as well as where the integer for this is

int x = 1;

it will be exactly the same machine code.

john
Jul 22 '05 #10
On Sat, 19 Jun 2004 03:50:03 GMT in comp.lang.c++, cppaddict
<he***@hello.com> wrote,
Ater I create my object, my methods can refer to things
like TRUNC_TOP and TRUNC_BOTTOM, can they not? And they could cast
those values to an int, too. If there is no memory allocated for
them, where is that int coming from?


The compiler pulls it out of the air. It comes from the same place 3
comes from in
if (i == 3) {

Note that it's illegal to write
int *i = &3;
because 3 has no address.

Jul 22 '05 #11

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

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
1
by: Dave Arkley | last post by:
I have a type Dictionary<int, List<int>> oKP = new Dictionary<int, List<int>>(); Im populating the dictionary so that a key is associated with a list of integers. I then remove some, or...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
5
by: mhuhn.de | last post by:
I am using XML Schema for quite a while but haven't written a single line of Relax NG yet. For what I know, I cannot define static content within an XML Schema. For example, I have the following...
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
1
by: Visame | last post by:
The following example is from C++ Standard(ISO14882) 9.4.2 Static data members class process { static process* run_chain; static process* running; }; process* process::running =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.