0

I have an ENUM for parameters of a method. This ENUM is a private member of a class, say

class Method

I have two variants for this method. List of parameters is almost the same. For the 2nd variant of the method, 3 parameters are to be appended to the list of parameters for first variant.

I would like to do it with a boolean, that is set to true for the 2nd variant. Is it possible to have something like this:

enum EcolId {
    P1,
    P2,
    P3,

    if(m_bool)
    {
         P4,
         P5
    }
}

if not, what should I do?

I have no other choice but use an ENUM here.

Thanks

kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • 2
    Use two different enumerations? Or two different classes, possibly inheriting from a common base class, each with the correct enumeration member? – Some programmer dude Mar 11 '13 at 11:39
  • Have one enum and validate the input to your function? This may be solved better using polymorphism, but we'd need more details to suggest a sensible solution. – Alex Chamberlain Mar 11 '13 at 11:40
  • This could help http://stackoverflow.com/questions/644629/base-enum-class-inheritance –  Mar 11 '13 at 11:51
  • @aleguna thanks, however i want to have this 2-variant enum in one single class, because my design is with one class and one computation method, with flag set to variant 1 or variant 2 – kiriloff Mar 11 '13 at 13:57

2 Answers2

2

No, you can't do THAT.

You could do:

 enum EcolId {
     P1,
     P2,
     P3,

 #ifdef SOMETHING
     P4,
     P5
 #endif 
 }

Alternatively, you could use a template. Something like this:

enum EcolId1
{
    P1,
    P2,
    P3,
};

enum EcolId2
{
    P1,
    P2,
    P3,
    P4,
    P5,
};


template<typename T>
class Method
{
     T enumvar;
     ... 
}

...
Method<EcolId1>  limited;
Method<EcolId2>  more;     
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • i like first solution. what is SOMETHING ? a global variable ? where should it be `defined` but also set to true or false ? i would like to init this variable in my constructor, where one argument is the bool value for the boolean controlling the enum. can this be done that way ? – kiriloff Mar 11 '13 at 11:51
  • No, you must use a #define, which has to be known at compile-time. – Mats Petersson Mar 11 '13 at 11:55
  • thanks. can i have this: somewhere in .h file, `#define STHG true`. then the constructor `Method::Method(bool b){STHG = b;}` ? – kiriloff Mar 11 '13 at 12:00
  • i guess it is bad idea since it is not known at compile time then – kiriloff Mar 11 '13 at 12:09
  • then second solution is only one solving my problem. I solved another way using one enum and tricks in other parts of code so that uneeded params do not hurt. thanks anyway – kiriloff Mar 11 '13 at 14:45
0

Depends on how the variant is motivated.

Why not use two enums?

As an alternative, you can generate an error in the first variant when non-handled values are used.

What irritates me a bit is that you talk about the list of parameters which gets extended. I hope you mean the list of values for one parameter which you want to extend.

class stacker
  • 5,357
  • 2
  • 32
  • 65