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);
?
An alternative to inheritance could be to do aClass a template :
aClass
template
template <bool T> class aClass { public: bool b; aClass(): b(T) {} }; typedef aClass<true> stateTrue; typedef aClass<false> stateFalse;
No, because it is an instance, not a type.
You can get:
class stateTrue: public aClass { public: stateTrue() : aClass(true) {} };
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 };