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

4
  • 2
    Use two different enumerations? Or two different classes, possibly inheriting from a common base class, each with the correct enumeration member? Commented Mar 11, 2013 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. Commented Mar 11, 2013 at 11:40
  • This could help stackoverflow.com/questions/644629/base-enum-class-inheritance Commented Mar 11, 2013 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 Commented Mar 11, 2013 at 13:57

2 Answers 2

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;     
Sign up to request clarification or add additional context in comments.

5 Comments

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 ?
No, you must use a #define, which has to be known at compile-time.
thanks. can i have this: somewhere in .h file, #define STHG true. then the constructor Method::Method(bool b){STHG = b;} ?
i guess it is bad idea since it is not known at compile time then
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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.