I am wondering what was the best way using C++ to make a class constructor take an argument from a limited list. For example, if I have a class called Colour
, then the constructor would only accept Red
, Green
, or Blue
and store the value to a private variable. Then I would have a method called printColour
which would print the variable.
Currently, I've been able to get this to work:
#include <iostream>
using namespace std;
class MyClass
{
public:
typedef enum { A = 3, B = 7 } Options_t;
MyClass(Options_t);
void printVal(void);
private:
Options_t _val;
};
MyClass::MyClass(Options_t val)
{
_val = val;
}
void MyClass::printVal(void)
{
cout << _val << endl;
}
MyClass hi(MyClass::A);
int main(void)
{
hi.printVal();
return 0;
}
However, the ::
notation to access the class's struct seems a bit clunky (I want to write an Arduino Library so it might be beginners needing to use the constructor / methods.
Is there any better way to do this (maybe with .
notation?) Thanks!
Edit: When I try to compile with MyClass x(2)
(int
instead of Options_t
), I get this error:
class_enums.cpp:24:9: error: no matching constructor for initialization of 'MyClass'
MyClass hi(2);
^ ~
class_enums.cpp:4:7: note: candidate constructor (the implicit copy constructor) not viable: no known
conversion from 'int' to 'const MyClass' for 1st argument
class MyClass
^
class_enums.cpp:14:10: note: candidate constructor not viable: no known conversion from 'int' to
'MyClass::Options_t' for 1st argument
MyClass::MyClass(Options_t val)
^
So it seems that I can't just use an int as the argument?