473,396 Members | 2,052 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.

typedefed enum

PSN
can any one tell me if there is a way to redeclare a typedef enum to
add new elements at the end ..

Ex: "test.h"
typedef enum test1
{
hcone = 0,
hctwo
hcthree,
hcfour,
hcfive,
hcsic,
}test;

redeclare it to ....

File: "test.h"
typedef enum test1
{
hcone = 0,
hctwo
hcthree,
hcfour,
hcfive,
hcsic,
hcSIZE
}test;

Whatever the solution is i just want to add new elements !!

thanks

May 4 '06 #1
1 2534
PSN wrote:
can any one tell me if there is a way to redeclare a typedef enum to
add new elements at the end ..

Ex: "test.h"
typedef enum test1
{
hcone = 0,
hctwo
hcthree,
hcfour,
hcfive,
hcsic,
}test;

redeclare it to ....

File: "test.h"
typedef enum test1
{
hcone = 0,
hctwo
hcthree,
hcfour,
hcfive,
hcsic,
hcSIZE
}test;

Whatever the solution is i just want to add new elements !!

thanks


First in C++, using the typedef here is unnecessary. Just do this to
acheive the same effect:

enum test
{
// ...
};

However, you can't add new elements to an existing enum. You'll either
have to modify the existing definition, or, if that is not allowed,
create a new enum. You can use the same names if you put it in a
different scope. For instance:

typedef enum test1
{
hcone = 0,
hctwo
hcthree
} test;

namespace MyTest
{
enum test
{
hcone = ::hcone,
hctwo = ::hctwo
hcthree = ::hcthree,
hcsize
};
}

Which you can use fully qualified:

void Foo( MyTest::test );

Instead of adding the size to the end, however, I generally prefer to
have a separate enum because the size is technically an invalid value
in the enum that could be used like this:

Foo( MyTest::hcsize );

Because of this, every user of your enum (e.g. Foo) needs to check the
value of the enum to make sure it is within range (i.e., != hcsize). If
instead you do this:

enum MyEnum
{
MY_MIN,
MY_ONE = MY_MIN,
MY_TWO,
MY_THREE,
MY_MAX = MY_THREE
};
enum { MY_SIZE = (MY_MAX - MY_MIN) + 1 };

Then you avoid the problem mentioned above, but you know the size and
can easily iterate on the enum values (assuming the enum values go in
order, of course):

std::vector<MyEnum> v( MY_SIZE, MY_ONE );
// ...

for( int m=MY_MIN; m <= MY_MAX; ++m )
{
// ...
}

// Or if you need m to be of the enum type...
for( MyEnum m=MY_MIN; m <= MY_MAX; m = MyEnum( m + 1 ) )
{
// ...
}

Unfortunately, with this technique, you have to remember to update the
min and max values if you change the enum, but that is still easier
IMHO than relying on the user to check the validity of the enum value,
which is all too easy to omit.

Cheers! --M

May 4 '06 #2

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

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
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...

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.