Reputation: 1959
There is an existing enum
typedef enum
{
myEnum_front = 11,
myEnum_back = 19
} myEnumSides;
I want to create another enum new_myEnumSides
and it's values should be mapped to the values of myEnumSides
. Hence forth I would be using new_myEnumSides
instead of myEnumSides
.
Is the code below ok for this purpose?
typedef enum
{
new_myEnum_front = myEnumSides::myEnum_front,
new_myEnum_back = myEnumSides::myEnum_back
} new_myEnumSides;
Or is there a better way?
Upvotes: 5
Views: 9477
Reputation: 31489
It's usually the case that you don't want to expose details of third party libraries, but an enumeration that lies there might have the exact members you need.
In that case it's beneficial to create a mapping for the third party enum
, so that even if the backend library changes, you can simply provide the enumerators yourself.
The most concise way to do this, is by creating an alias:
using MyEnum = ThirdPartyEnum;
// {
// enumerator1,
// enumerator2
// }
This way, user code won't rely on third party internals,
you avoid doing conversions and even if the third party library is replaced, your users can keep using MyEnum
by simply uncommenting the enumerator values (I like to keep them in comments, so users won't have to refer to third party libraries for documentation.
Upvotes: 0
Reputation: 18850
The only reason I can imagine you wanting to do this is to extend an existing enum
and create a new one with extra values.
As enum
does not offer any form of inheritance, being compile time constants and really just integers, you could do something like this, although I don't particularly recommend it..
// internalEnums.h
myEnum_front = 11,
myEnum_back = 19
// customerEnums.h
someNewValue = 20,
someOtherNewValue = 21
// Wherever you want to define your enums
typedef enum
{
#include "customerEnums.h"
} customerAccessible;
typedef enum
{
#include "internalEnums.h"
#include "customerEnums.h"
} internalUseOnly;
// Now there are two enumerations, sharing items.
customerAccessible::someNewValue // 11
customerAccessible::myEnum_front; // Doesn't exist
EDIT As per your comment, you could expose enumerations to the customer by keeping values in a header specifically for external use.
Upvotes: 0
Reputation: 29966
I can't possibly imagine why do you need to do it... If you don't need to rename the enum's values, you can just make another variable of the first one, without adding another enum (I believe this is not your case, but still have to point out this opportunity):
typedef enum
{
myEnum_front = 11,
myEnum_back = 19
} myEnumSides, new_myEnumSides;//<-- just add the new variable here
If you do want to rename it (which I believe, is your case), you should not use the ::
operator, but simply write:
typedef enum
{
myEnum_front = 11,
myEnum_back = 19
} myEnumSides;
typedef enum
{
new_myEnum_front = myEnum_front,
new_myEnum_back = myEnum_back
} new_myEnumSides;
The ::
operator should be used only if the enum is inside a class, structure or namespace, otherwise no ::
is needed.
Upvotes: 1