I'm looking for a way to reduce the value of a variable that takes its value from a function. However, until now I haven't thought of anything.
The way it is written, the code currently will always return the value of the function.
std::string Barger::ShowMessage() {
int var = Barger::get().var();
char mes[20];
sprintf_s(mes, sizeof(mes), "Value Var %d", var--);
return std::string(mes);
}
What I need is that every time the std::string Barger::ShowMessage()
function is executed it will be reduced by -1 from the variable, but the function Barger::get().var()
is not allowing this.
You are not saving the result of operator--
anywhere other than in a local variable, so every time you call ShowMessage()
, you are retrieving the same original value again and again. You need something more like the following, which you will have to add if you do not already have it:
std::string Barger::ShowMessage() {
int var = Barger::get().var();
char mes[20];
sprintf_s(mes, sizeof(mes), "Value Var %d", var--);
Barger::get().setvar(var); // <-- here
return std::string(mes);
}
Unless you can change var()
to return a non-const int&
reference to some internal int
variable, rather than returning an int
copy of it:
std::string Barger::ShowMessage() {
int& var = Barger::get().var();
char mes[20];
sprintf_s(mes, sizeof(mes), "Value Var %d", var--);
return std::string(mes);
}