'In c++, why does the compiler choose the non-const function when the const would work also? [duplicate]
For example, suppose I have a class:
class Foo
{
public:
std::string& Name()
{
m_maybe_modified = true;
return m_name;
}
const std::string& Name() const
{
return m_name;
}
protected:
std::string m_name;
bool m_maybe_modified;
};
And somewhere else in the code, I have something like this:
Foo *a;
// Do stuff...
std::string name = a->Name(); // <-- chooses the non-const version
Does anyone know why the compiler would choose the non-const version in this case?
This is a somewhat contrived example, but the actual problem we are trying to solve is periodically auto-saving an object if it has changed, and the pointer must be non-const because it might be changed at some point.
Solution 1:[1]
Because a is not a const pointer. Therefore, a non-const function is a closer match. Here is how you can call the const function:
const Foo* b = a;
std::string name = b->Name();
If you have both a const and a non-const overload, and want to call the const one on a non-const object, this might be an indication of bad design.
Solution 2:[2]
The compiler does not take into account how you are using the return value in its determination; that's not part of the rules. It doesn't know if you're doing
std::string name = b->Name();
or
b->Name() = "me";
It has to choose the version that works in both cases.
Solution 3:[3]
You can add a "cName" function that is equivalent to "Name() const". This way you can call the const version of the function without casting to a const object first.
This is mostly useful with the new keyword auto in C++0x, which is why they are updating the library to include cbegin(), cend(), crbegin(), crend() to return const_iterator's even if the object is non-const.
What you are doing is probably better done by having a setName() function that allows you to change the name rather than returning a reference to the underlying container and then "maybe" it is modified.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mark Ransom |
| Solution 3 | Greg Rogers |
