Typedef class with a specific constructor argument

Let's start with a simple class in C ++:

class aClass {
        bool b;
        aClass(bool x){b=x;} 
};

      

Is it possible to introduce typedef 2 new types stateTrue and stateFalse so that if I do:

stateTrue variable;

      

it translates to:

aClass  variable(true); 

      

?

+3


source to share


3 answers


An alternative to inheritance could be to do aClass

a template

:



template <bool T>
class aClass
{
public:
    bool b;
    aClass(): b(T) {}
};

typedef aClass<true>  stateTrue;
typedef aClass<false> stateFalse;

      

+5


source


No, because it is an instance, not a type.

You can get:



class stateTrue: public aClass {
public:
    stateTrue() : aClass(true) {}
};

      

0


source


The closest will be

class stateTrue : // new type needed, not just a new name
  public aClass { // but obviously can be converted to aClass
  public: stateTrue() : aClass(true) { } // Default ctor sets aClass base to true
};

      

0


source







All Articles