The article, apparently, considers a lot of basic premises, but the author still raises the question of constant and non-constant overloading of functions that return pointers. The last line of the article:
Many will probably answer: "It depends." but I would like to ask: "It depends on what?"
To be absolutely accurate, it depends on whether the state of the object of object A is logically part of the state of the object this .
As an example, where it is located, vector<int>::operator[] returns a reference to int. Int referand is a "part" of the vector, although it is not actually a member of the data. So the const-overload idiom is applied: change the element and you have changed the vector.
For an example where this is not the case, consider shared_ptr . It has a member function T * operator->() const; , because the constructive pointer const for a non-constant object makes logical sense. Referand is not part of the smart pointer: changing it does not change the smart pointer. Thus, the question of whether it is possible to βresetβ a smart pointer to a call to another object does not depend on whether the link is and is const.
I donβt think that I can provide any complete recommendations allowing you to decide whether the bridgehead is logically part of the object or not. However, if changing the pointer changes the return values ββor other behavior of any member functions from this , and especially if the bridgehead is involved in operator== , then the likelihood that it is logically part of the this object.
I would be mistaken in assuming that it is part (and provides overload). Then, if there was a situation where the compiler complains that I am trying to change the object A returned from the const object, I would think if I really should do this or not, and if this changes the design, so only the -to-A pointer conceptually, it is part of the state of the object, not A. itself. This, of course, requires ensuring that the modification of A does nothing that violates the expected behavior of this const object.
If you publish an interface, you may have to figure this out beforehand, but in practice, returning from const overloads to const-function-return-not-const-pointer is unlikely to break the client code. In any case, by the time you publish the interface, which we hope have used it a bit, and probably felt that it really included the state of your object.
Btw, I'm also trying to make a mistake on the side of not providing pointers / reference accessories, especially modifiable ones. This is a really separate issue (the Law of Demeter and all that), but the more times you can replace:
A *getA(); const A *getA() const;
from:
A getA() const;
The less you have to worry about the problem. Of course, the latter has its limitations.