An object of type Child*
cannot be bound to a Parent*&
for exactly the same reason that a Child**
cannot be converted to a Parent**
. Allowing it would allow the programmer (intentionally or not) to break type safety without a cast.
class Animal {};
class DangerousShark : public Animal {};
class CuteKitten : public Animal {};
void f(Animal*& animalPtrRef, Animal* anotherAnimalPtr)
{
animalPtrRef = anotherAnimalPtr;
}
void g()
{
DangerousShark myPet;
CuteKitten* harmlessPetPtr;
f(harmlessPetPtr, &myPet); // Fortunately, an illegal function call.
}
Edit
I think that some of the confusion arises because of the loose use of the words 'convert' and 'conversion'.
References can't be rebound, unlike objects which can be reassigned, so in the context of references when we speak of conversion we can only be concerned about initializing a new reference.
References are always bound to an object, and from the OP's question it was clear that he is aiming to get a reference that is a direct bind to an existing object. This is only allowed if the object used to initialize the reference is reference-compatible with the type of the reference. Essentially, this is only if the types are the same, or the type of the object is derived from the type of the reference and the reference type is at least as cv-qualified as the initializing object. In particular, pointers to different types are not reference-compatible, regardless of the relationship of the pointed-to types.
In other cases, a reference can be initialized with something that can be converted to the reference type. In these cases, though, the reference must be const and not volatile and the conversion will create a temporary and the reference will be bound to this temporary and not the original object. As pointed out, this is not suitable for the requirements of OP's motivating example.
In summary, a Child
can be bound directly to a Parent&
but a Child*
cannot be directly bound to a Parent*&
. A Parent* const&
can be initialized with a Child*
, but the reference will actually bind to a temporary Parent*
object copy-initialized from the Child*
object.