I was trying to answer the question mentioned here by passing the reference to the pointer instead of pointer to pointer like this:
class Parent
{
};
class Child : public Parent
{
};
void RemoveObj(Parent*& pObj)
{
delete pObj;
pObj = NULL;
}
int main()
{
Parent* pPObj = new Parent;
Child* pCObj = new Child;
pPObj = new Parent();
pCObj = new Child();
RemoveObj(pPObj);
RemoveObj(pCObj); // This is line 32
return 1;
}
But this produces the following compiler error at line 32:
error C2664: ‘RemoveObj’ : cannot
convert parameter 1 from ‘Child *’ to
‘Parent *&’
I agree that conversion from Child** to Parent** is not allowed. But why this conversion is also not allowed?
An object of type
Child*cannot be bound to aParent*&for exactly the same reason that aChild**cannot be converted to aParent**. Allowing it would allow the programmer (intentionally or not) to break type safety without a cast.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
Childcan be bound directly to aParent&but aChild*cannot be directly bound to aParent*&. AParent* const&can be initialized with aChild*, but the reference will actually bind to a temporaryParent*object copy-initialized from theChild*object.