473,671 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2553
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<MyE num> 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
4835
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 EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
21
4588
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
3590
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(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
18
11343
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
3392
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 and return the name "FirstData"
13
12376
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
2456
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 installed the boost library. This is using gcc under FC3.
2
2118
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 installed the boost library. This is using gcc under FC3.
34
11179
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
8483
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
8401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8603
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,...
0
7444
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
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
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2818
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
1815
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.