Two functions doing the exact same thing, alias?
In a derived class I have a function called evaluate() (it's a virtual in the base class). In this derived class i also have a function set_value() and hence i want 开发者_如何学编程get_value() as well. get_value() should return the exact same thing as evaluate()
Is there anyway to say that a call to get_value is a call to evaluate()? With some sort of alias keyword?
I don't know if this exists or what it would be called, I have searched and nothing found.
Maybe I shoud do:
inline double Variable::get_value() const
{
return evaluate();
}
Nope, there are no aliases in C++ you're searching for. Sure, that is the way:
double Variable::get_value() const
{
return evaluate();
}
On the other hand you could make get_value()
function in a superclass and let it do the same thing if it is your design requirement.
The another advantage of implementing get_value()
this way with bare hands is to provide an opportunity to involve additional logic. In case your evaluate()
will get more and more CPU time you might implement simple caching in-place.
精彩评论