I have two member function in a class one of them simply return one of the memeber variables. For example:
int my_class::get_value() const{
return this->value;
}
The other function do a very complex operation on value in order to return some other value. For example:
int my_class::get_other_value() const{
int other_value=value;
//many complex operation on other_value (O(N^3))
return other_value;
}
They both seem from declaration as simple functions that return some value. However, this could lead to a serious problem when the coder who call them treat them as the same. For example:
int main(){
my_class foo;
//fill foo in some way;
std::cout<< foo.get_value() << 2*foo.get_value();
std::cout << foo.get_other_value() << 2*foo.get_other_value();
}
The first cout is fine. However, the second one is miserable (unless the compiler optimize it).
The question is:
Firstly, is it ok to make the computing in a get member function ? Should I separate them to two member function and one member variable? one called compute and the other called get and the member variable hold the computed value?
Secondly, what is the standard way of naming it in order to by default tell the one who uses my_class that the member function get_other_value is not a simple getter?.
Aucun commentaire:
Enregistrer un commentaire