D
DGG
I have a maybe novice question to ask for expert advice.
1. How to define class-wide enums inside a class?
For example, currently I can think of a not satisfactory
implementation.
class MyWidget {
public:
const static int RELEASED = 0;
const static int ARMED = 1;
const static int PRESSED = 2;
const int option;
.....
public:
MyWidget() { state = RELEASED; }
public:
changeState(int newState);
setOption1() {option = 1;}
setOption2() {option = 2;}
private:
int state;
}
main()
{
MyWidget w1;
.....
w1.changeState(MyWidget::ARMED);
....
w1.setOption2();
}
But what I would like is to define a enum TYPE that is "inside"
the MyWidget class, so I can change the changeState() function's
signature to something like changeState(MyWidget::WidgetState
newState). Is this a better way? and How can I do this?
2. The usage of "const".
Again with the same example, I want to set the instance variable
"option" only once for each instance. Does defining it as const help?
and will it work? Ie., I don't know whether setOption1() or
setOption2() will be called, just content that only the first setting
is observed throught the life of one instance.
Thanks for any help
1. How to define class-wide enums inside a class?
For example, currently I can think of a not satisfactory
implementation.
class MyWidget {
public:
const static int RELEASED = 0;
const static int ARMED = 1;
const static int PRESSED = 2;
const int option;
.....
public:
MyWidget() { state = RELEASED; }
public:
changeState(int newState);
setOption1() {option = 1;}
setOption2() {option = 2;}
private:
int state;
}
main()
{
MyWidget w1;
.....
w1.changeState(MyWidget::ARMED);
....
w1.setOption2();
}
But what I would like is to define a enum TYPE that is "inside"
the MyWidget class, so I can change the changeState() function's
signature to something like changeState(MyWidget::WidgetState
newState). Is this a better way? and How can I do this?
2. The usage of "const".
Again with the same example, I want to set the instance variable
"option" only once for each instance. Does defining it as const help?
and will it work? Ie., I don't know whether setOption1() or
setOption2() will be called, just content that only the first setting
is observed throught the life of one instance.
Thanks for any help