Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I have created one struture like,
typedef enum
{
   NAME = 0,
   ADDRESS,
   BIRTHDATE,
}INFORMATION

And I want to get Number of elements in this structure. Is there any function?
Posted
Updated 26-Feb-19 23:49pm
v3

Considering the structure that is given by you is:

typedef enum
{
MIN = 0,
NAME  = MIN,
ADDRESS,
BIRTHDATE,
MAX = BIRTHDATE
};
int NumOfElement = MAX-MIN+1;


Or directly BIRTHDATE + 1 (if Enum start with 0).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Jun-11 3:12am    
This is improved variant of what I suggested, my 5.
All problems we're discussing here is C/C++ legacy of enum type which is not "real" enumeration type. All the details in my article "Enumeration Types do not Enumerate!":
http://www.codeproject.com/KB/dotnet/EnumTypesDoNotEnumerate.aspx

--SA
ShilpiP 8-Jun-11 3:22am    
Thanks for 5. I was unaware of your discussion. Let me check.
CPallini 8-Jun-11 5:53am    
I would prefer NAME=0, MIN=NAME.
In the way you asked ... NO.
There is no function (code to be executed) that can give such a result, since standard C++ has no reflection.
The standard RTTI is tailored to support dynamic_cast and have no role on enums.

Things may be different if we consider some language extensions (like C++/CLI, that is an ECMA standard not accepted by ANSI that is the owner of the C++ standard ...) that can add reflection and hence the capability to query at runtime the state of the source.

Of course there can be tricks like
enum E
{
    a,b,c, count;
};

That leads to a=0, b=1, c=2, count=3. But that's a different thing respect to what you asked (may be not adequate, for certain purpose).
 
Share this answer
 
v2
I am agree with SA, however while doing generic programming and thinking about future extensibility, we always add eENUMLast in enum, so that if we add/delete any enumerated value, eENUMLast always remain last.
 
Share this answer
 
You rather need a simple technique commonly used in such cases.

C++
enum Information {
   NAME = 0,
   ADDRESS,
   BIRTHDATE,
   Length,
};

//...

for (int index = NAME; index < Length; ++index) {
   // use index as 
}


—SA
 
Share this answer
 
v2
Comments
Resmi Anna 8-Jun-11 1:36am    
Why should we go for this?
int NumberOfElm = Length - NAME;
is enough rt?
what if the enum looks like
enum Information {
NAME = 0,
ADDRESS,
BIRTHDATE = 100,
Length,
};
also i feel this question doesnt make any sense like number of elements in array or list. because i havent heard of anything like dynamic enum in c++. is there anything like that??
Niklas L 8-Jun-11 2:30am    
IMO it's rather clear that the OP doesn't have BIRTHDATE = 100. The dynamic enums are not dynamic during runtime, but rather during the evolution of the software. I can think of several situations where this comes in handy. Not being able to tell the number of items in an enumerable sequence is a severe limitation.
Resmi Anna 8-Jun-11 2:42am    
what role the number of elements in enum plays?Its value is important rt?
sorry if i am wrong.
any real situation that needs the number of elemnts in enum?
Niklas L 8-Jun-11 3:08am    
You can use the enum values to index arrays, in which case knowing the bounds is crucial. Implementation of a deck of cards is a classical example. It is not likely however, that someone will introduce a new suit with a new version of the software, but imagine the same thing where the enum is representing a state which can be expanded in future versions. Not being able to programatically tell the number of items in the sequence is a great source for errors.
Resmi Anna 8-Jun-11 4:56am    
Not here for a dispute:):)..just to share our views.

Deck of card is classical example on the usage of enum.

I am asking is there any point at which we need to know the number of elements in an enum dynamically?

like suppose we have a character array in our programchar str[50];

At execution point 1 it may contain the string"Hi"

At execution point 2 it may contain the string"Hello"

so we should have some function to know the length of an array.

correct till here??

suppose your array conatins the value "Hi" throught the program execution.If are quite sure about that, you can use

const int Lenth = 2;

No need to write a GetLenght() function.It will only add a performance hit.

agreeing??suppose your Hi becomes "Hello" in your software version 2.

then you can modify const int Lenth = 5.

here also we consider performance than avoiding one line codeagreeing??

so like this is there any chance that at execution point1 your enum contains 4 elemnts and at execution point 2 enum conatins 5 elemnts?Then only you need to bother about calculating

the number of items of an enum rt?someone understood:(:(?????
Enum.GetNames(typeof(INFORMATION)).Count()


It will be helpful. Even though you will increase decrease values in enum It will give correct result.
 
Share this answer
 
Comments
Maciej Los 27-Feb-19 6:33am    
My vote of 1. This is not a C++ code!
A C++ version is bit different. See: Enum.GetNames(Type) Method (System) | Microsoft Docs[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900