const "getter" but updating on request?
-
Looking for an idea solving following problem: I have a "get()" function which returns the condition of an object stored in a member variable. There is also some code that updates this _cond
int getCond() const { return _cond; }
void UpdateCond() { ... etc ... _cond=f(); ... }In the whole code getCond() and UpdateCond() called are independetly, so that a "const" function could call "getCond()" But there is one silly stupid use of the getCond() where the Update() is done in the getCond() code So the former old coder maybe thought "update on request!"
int getCond() { UpdateCond(); return _cond; }
because UpdateCond() changes the member _cond getCond() cannot be const anymore. Anyone an idea to overcome this problem? (if not I have to change back this crunchy design into non-const getCond() functions... There are about 100 in derived classes ...
-
Looking for an idea solving following problem: I have a "get()" function which returns the condition of an object stored in a member variable. There is also some code that updates this _cond
int getCond() const { return _cond; }
void UpdateCond() { ... etc ... _cond=f(); ... }In the whole code getCond() and UpdateCond() called are independetly, so that a "const" function could call "getCond()" But there is one silly stupid use of the getCond() where the Update() is done in the getCond() code So the former old coder maybe thought "update on request!"
int getCond() { UpdateCond(); return _cond; }
because UpdateCond() changes the member _cond getCond() cannot be const anymore. Anyone an idea to overcome this problem? (if not I have to change back this crunchy design into non-const getCond() functions... There are about 100 in derived classes ...
FriendOfAsherah wrote:
But there is one silly stupid use of the getCond() where the Update() is done in the getCond() code
There is only one clean solution: Remove the wrong implementation and replace calls of it by two calls to
UpdateCond()
andgetCond()
. You may also changeUpdateCond()
to return the state instead of returning nothing. This will not affect existing code callingUpdateCond()
but allows you to use it instead of the non-constgetCond()
function. -
FriendOfAsherah wrote:
But there is one silly stupid use of the getCond() where the Update() is done in the getCond() code
There is only one clean solution: Remove the wrong implementation and replace calls of it by two calls to
UpdateCond()
andgetCond()
. You may also changeUpdateCond()
to return the state instead of returning nothing. This will not affect existing code callingUpdateCond()
but allows you to use it instead of the non-constgetCond()
function.seperating is really a work for months because this is somewhere in a 10x hierarchie and 100x parallel used chaotic class inheritance... A { getCond() astract }; <- B <- C <-D ... about 10 Steps A <-B1 ... A <- B2 ... and one of these C´s contains the thing with condUpdate() I inherited this code ... hating it .. maybe mutual or const_cast can help? but still update() is a non const function I found another solution now which can work with the "non const" version which is used in the code... But cleaning up this will be needed somedays..
-
seperating is really a work for months because this is somewhere in a 10x hierarchie and 100x parallel used chaotic class inheritance... A { getCond() astract }; <- B <- C <-D ... about 10 Steps A <-B1 ... A <- B2 ... and one of these C´s contains the thing with condUpdate() I inherited this code ... hating it .. maybe mutual or const_cast can help? but still update() is a non const function I found another solution now which can work with the "non const" version which is used in the code... But cleaning up this will be needed somedays..
I understand that it is a lot of work. But doing it the wrong way will make it even more complicated for future updates (especially when those have to be done by others). The trick here is finding the code that calls this single non-const function (and expects that an update is performed) and replace that. You may use your development tools to find these calls (many have options like "Find Usages").