This question discusses how the arrow operator ->
is automatically re-applied to the return value of an overloaded operator->()
until the value returned is a raw pointer, at which point the raw pointer is dereferenced as if with ->
. However, the same is not true when one has a pointer-to-pointer type that they wish to dereference to the base value - they must use (*ptr_to_ptr)->foo()
instead. It seems to me that usage as ptr_to_ptr->foo()
would be unambiguous, even more so than the automatic re-application of ->
onto returned values until a raw pointer is returned. So, what was the reasoning behind this decision?
minimal working example:
#include <iostream>
struct Dog {
void bark() { std::cout << "woof!" << std::endl; }
};
struct DogWalker {
Dog* dog;
Dog* operator->() {
return dog;
}
};
struct DogOwner {
DogWalker walker = { new Dog() };
DogWalker operator->() {
return walker;
}
};
void main()
{
DogOwner owner;
owner->bark(); // works, prints "woof"
Dog** ptr_to_ptr = new Dog*;
*ptr_to_ptr = new Dog;
(**ptr_to_ptr).bark(); // works
(*ptr_to_ptr)->bark(); // works
//ptr_to_ptr->bark(); // ERROR
//C2227: left of '->bark' must point to class/struct/union/generic type
}