C++ get method - returning by value or by reference
- by HardCoder1986
Hello! I've go a very simple question, but unfortunately I can't figure the answer myself.
Suppose I've got some data structure that holds settings and acts like a settings map.
I have a GetValue(const std::string& name) method, that returns the corresponding value.
Now I'm trying to figure out - what kind of return-value approach would be better. The obvious one means making my method act like
std::string GetValue(const std::string& name)
and return a copy of the object and rely on RVO in performance meanings.
The other one would mean making two methods
std::string& GetValue(...)
const std::string& GetValue(...) const
which generally means duplicating code or using some evil constant casts to use one of these routines twice.
#Q
What would be your choice in this kind of situation and why?