c++ - Safe getter to QStates -
i have class, containing qstatemachine
. has couple of qstate*
s.
private: qstatemachine m_machine; qstate* m_state1 qstate* m_state2 ...
i initialize state machine in constructor, , set machine running.
as states private, want allow user subclass , altering behavior (e.g. adding transitions, change properties, connecting signals e.t.c) want add getters. don't add setters, documentation states:
removing states while machine running discouraged.
the qtcreator produces that:
qstate *myclass:state1() const { return m_state1; }
which seems nice.
however seems me circumvents decision on not providing setters, possible:
qstate* state = state1(); *state = qstate([...]);
which understanding removes original state1
, overwrites new state.
so thought return const qstate*
instead.
const qstate* myclass::state() const { return m_state1; }
which seems work (the example above throw compiler error). new c++ not sure know have done there , if there other implications.
what right way achieve desired behavior?
if want user able retrieve state, unable affect retrieved state object, can consider using constant reference return type
const qstate& myclass::state() const { return *m_state1; }
in case, returned object of type const qstate&
, cannot assigned or receiver object of non-const function call. right way achieve behavior, returning const qstate*
result in identical behavior, require de-reference ->
each access.
wiki
Comments
Post a Comment