I have class, containing a QStateMachine
. It also has a couple of QState*
s.
private:
QStateMachine m_machine;
QState* m_state1
QState* m_state2
...
I initialize the state machine in the constructor, and set the machine running.
As the states are private, but I want to allow a user to subclass and altering some behavior (e.g. adding transitions, change properties, connecting to signals e.t.c) I want to add some getters. I don't add setters, as the documentation states:
Removing states while the machine is running is discouraged.
The QtCreator produces something like that:
QState *MyClass:state1() const
{
return m_state1;
}
which seems nice.
However it seems to me that this also circumvents my decision on not providing setters, as something like that is possible:
QState* state = state1();
*state = QState([...]);
which to my understanding removes the original state1
and overwrites it with a new state.
So my thought was to return a const QState*
instead.
const QState* MyClass::state() const
{
return m_state1;
}
Which seems to work (the example above will throw a compiler error). However I am that new to C++ that I am not sure that I know what I have done there and if there are other implications.
What is the right way to achieve my desired behavior?