user1709175
user1709175

Reputation:

Reference Semantics in C++

While there are zillions of sources on clarifying the concept of references in C++, I'm trying to explain the concept of reference to someone who is familiar with pointers. In other words, I'm wondering whether the following semantic expression is ALWAYS true?

TYPE& == *(TYPE const *)

Upvotes: 3

Views: 1080

Answers (5)

Dietmar Kühl
Dietmar Kühl

Reputation: 153955

The expression

TYPE& == *(TYPE const *)

looks a bit of domain error as TYPE& is a type while *(TYPE const*) looks like an expression applied to a type. At the very least, the right hand side should be const pointer rather than a pointer to const, i.e.:

A TYPE& behaves like an auto-dereferenced immutable pointer, something like *(TYPE* const) with the implicit constraint that the pointer cannot be null.

The compiler does recognize references and in some cases, namely when binding temporaries to a reference to a const object (T const&) at function scope: the life-time of the temporary gets expanded until the reference goes out of scope! For example:

{
    std::string const& s = std::string("longer lived");
    // the original temporary created above still lives
}   // <-- ... and gets destroyed when s goes out of scope here

Another major difference between pointers and references are semantics when it comes to operator overloading: pointers are consider built-in types and operators only involving built-in types cannot be overloaded. A reference of type T& behaves like a T object, i.e., overloaded operators for T& are considered. That is, while the a T& is identical to using a T* const from a representation point of view, the compiler understands the difference and treats the entities differently on a semantic level.

Upvotes: 2

sp2danny
sp2danny

Reputation: 7687

One of the ways it differs, is that you cannot take the address of a reference,
but you can take the address of a pointer. Since you can take the address of a
pointer, it must have storage. However, references do not have to have storage
(they are defined in the standard as aliases), and as often the compiler can,
they will in fact not have storage.

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145359

The stated possible type equivalence TYPE& == *(TYPE const *) does not make literal sense since the right hand side isn't a type, and since it's about a pointer to const instead of a const pointer.

However, considering the thing in an associative sense, one can imagine a rewrite rule where a reference declaration

T& r = o;

is rewritten as

T* const __p = &o;

and where every use of r is then rewritten as

(*__p)

Except for handling of the case where o is an rvalue expression it explains most and perhaps all practical properties of references.

However, as the relevant FAQ item “How can you reseat a reference to make it refer to a different object?” states,

please don't confuse references with pointers; they're very different from the programmer's standpoint

Upvotes: 0

user3344003
user3344003

Reputation: 21637

Probably the best way to explain references is to use Stroustroup's example showing how they allow creating functions that replace macros, something that would not be possible without references.

Upvotes: 0

Pranit Kothari
Pranit Kothari

Reputation: 9839

Yes, if you disassemble C++ code in Visual Studio, reference is nothing but const pointer. So both are almost equivalent.

But be aware, you cannot assign NULL (0/nullptr) to reference.

Upvotes: 0

Related Questions