The simplest version of the "problem" would be:
int x; int *p = &x; int* const& r1 = p; const int* const& r2 = p;
In this case, r1 bound directly to p , therefore &r1 == &p . However, r2 cannot directly bind to p . Instead, line r2 creates a temporary object const int * and binds r2 to a temporary one.
This is always an option when using const lvalue references; if the type is not suitable for direct binding, but there is an implicit conversion, then a temporary one can be created.
To avoid temporary use, you need to use a listing:
const int* const & r2 = const_cast<const int* &>(p);
or in the source code:
const C * const & r = const_cast<const C * &>(b.get_pC());
Note. He believed that bad style stores the address of what was passed by reference (since the caller does not expect this to happen, and the object may end in its life cycle). Think about redesigning your code, rather than using this const_cast solution; e.g. use C * const * get_pC() const { return &pA->pC; } C * const * get_pC() const { return &pA->pC; } and const C * const * r = b.get_pC();
To summarize the corresponding link binding rule: links are linked directly only if two types are the same (or a base class link can be associated with a derived class), and the type of the left side may have additional top-level qualifiers, for more details see here or [dcl .init.ref] in the Standard section.
source share