Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An object of type <code>Child*</code> cannot be bound to a <code>Parent*&amp;</code> for exactly the same reason that a <code>Child**</code> cannot be converted to a <code>Parent**</code>. Allowing it would allow the programmer (intentionally or not) to break type safety without a cast.</p> <pre><code>class Animal {}; class DangerousShark : public Animal {}; class CuteKitten : public Animal {}; void f(Animal*&amp; animalPtrRef, Animal* anotherAnimalPtr) { animalPtrRef = anotherAnimalPtr; } void g() { DangerousShark myPet; CuteKitten* harmlessPetPtr; f(harmlessPetPtr, &amp;myPet); // Fortunately, an illegal function call. } </code></pre> <p><strong>Edit</strong></p> <p>I think that some of the confusion arises because of the loose use of the words 'convert' and 'conversion'.</p> <p>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.</p> <p>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 <em>reference-compatible</em> 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.</p> <p>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.</p> <p>In summary, a <code>Child</code> can be bound directly to a <code>Parent&amp;</code> but a <code>Child*</code> cannot be directly bound to a <code>Parent*&amp;</code>. A <code>Parent* const&amp;</code> can be initialized with a <code>Child*</code>, but the reference will actually bind to a temporary <code>Parent*</code> object copy-initialized from the <code>Child*</code> object.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload