Why is there a need for reference in C++?

S

sarathy

Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

- Is there anything that a reference can do that a pointer cannot ???

Please clarify,
Sarathy
 
W

W Marsh

Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

- Is there anything that a reference can do that a pointer cannot ???

Please clarify,
Sarathy

Pointers cause excess friction, and are believed to have been the
cause of explosions in space craft. References were invented for safe
use aboard spacebound vehicles.
 
K

Kai-Uwe Bux

sarathy said:
Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

It wasn't fine, was it? Pointers *are* tricky. Now, in C++ the trickyness of
pointers is compounded by exceptions: since exceptions can divert the flow
of control at almost any point and toward unknown locations, the basic
requirement of a pointer that every new() is matched by a delete() along
each path of execution is harder to match. Thus, a device was created to
eliminate some uses of pointers. References and standard containers are in
this category. Other devices, like auto_ptr, were introduced to mitigate
the dangers for the remaining cases of pointer use.

- Is there anything that a reference can do that a pointer cannot ???

References can extend the life-time of temporaries:

#include <iostream>

struct log {

log ( void ) {
std::cout << "construction" << std::endl;
}

log ( log const & ) {
std::cout << "copy" << std::endl;
}

~log ( void ) {
std::cout << "destruction" << std::endl;
}

void access ( void ) const {
std::cout << "access" << std::endl;
}

};

log create_tmp ( void ) {
return ( log() );
}

int main ( void ) {
{
log const & ref = create_tmp();
ref.access();
}
std::cout << std::endl;
{
// warning: UB
log const * ptr = &create_tmp();
ptr->access();
}
}

// end of file


Best

Kai-Uwe Bux
 
D

Daniel T.

"sarathy said:
Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

I read "The Design and Evolution of C++" by Bjarne Stroustrup a long
time ago so I may not remember this correctly, however as I remember it
references were put in the language to make operator overload work right.
- Is there anything that a reference can do that a pointer cannot ???

class MyClass { };

MyClass operator+( const MyClass& lhs, const MyClass& rhs );

Without references, your only choices are to pass by value (which could
be quite expensive for objects of a big class, or pass by pointer which
would make the calling code look like:

MyClass a, b;
MyClass c = &a + &b;

Which seems rather clumsy.
 
P

Phlip

sarathy said:
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

Normal pointers have too many abilities, so they are high risk. We need a
feature with fewer abilities, so it's safer.
- Is there anything that a reference can do that a pointer cannot ???

It can refer to a temporary object.

It can create a syntax error when you abuse it in ways a pointer would
accept.

Think of a reference as another name for a target - an alias for a target.
Don't think of it as a different kind of pointer.

And, the next time you program a "handle" of some kind, if you don't need it
to be NULL, and don't need to index or increment it, use a reference. Put
another way, always use a reference unless you need a pointers' extra
features.
 
R

Roland Pibinger

I read "The Design and Evolution of C++" by Bjarne Stroustrup a long
time ago so I may not remember this correctly, however as I remember it
references were put in the language to make operator overload work right.

Interesting. And I thought they were introduced to make C++
programming more convenient and safer. "The Design and Evolution of
C++" really should be my next (and probably last) C++ book.

Best regards,
Roland Pibinger
 
J

Jerry Coffin

[ ... references ]
Interesting. And I thought they were introduced to make C++
programming more convenient and safer. "The Design and Evolution of
C++" really should be my next (and probably last) C++ book.

Yup, his memory was dead on in this case. D&E, 3.7, says: "References
were introduced primarily to support operator overloading."
 
L

Lally

By accepting a Foo& instead of Foo*, it's a nice way to get polymorphic
access to objects without having to constantly test for NULL.
 
P

Pierre Barbier de Reuille

Kai-Uwe Bux said:
It wasn't fine, was it? Pointers *are* tricky. Now, in C++ the trickyness of
pointers is compounded by exceptions: since exceptions can divert the flow
of control at almost any point and toward unknown locations, the basic
requirement of a pointer that every new() is matched by a delete() along
each path of execution is harder to match. Thus, a device was created to
eliminate some uses of pointers. References and standard containers are in
this category. Other devices, like auto_ptr, were introduced to mitigate
the dangers for the remaining cases of pointer use.



References can extend the life-time of temporaries:

But what is the difference between :


[...]
{
log const & ref = create_tmp();
ref.access();
}
[...]

and :

[...]
{
log val = create_tmp();
val.access();
}
[...]

Besides the fact in the first case you won't be able to modify the
reference ?

Pierre
 
A

Alf P. Steinbach

* Pierre Barbier de Reuille:
Kai-Uwe Bux said:
References can extend the life-time of temporaries:

But what is the difference between :

[...]
{
log const & ref = create_tmp();
ref.access();
}
[...]

and :

[...]
{
log val = create_tmp();
val.access();
}
[...]

Besides the fact in the first case you won't be able to modify the
reference ?

As Kai-Uwe wrote, the reference extends the lifetime of the temporary.
Here the type of the temporary can be a class derived from 'log'. The
non-reference copies the temporary to a variable of type 'log', possibly
slicing.
 
V

Victor Bazarov

Pierre said:
[..]
But what is the difference between :


[...]
{
log const & ref = create_tmp();
ref.access();
}
[...]

and :

[...]
{
log val = create_tmp();
val.access();
}
[...]

Besides the fact in the first case you won't be able to modify the
reference ?

In the first case you also won't be able to modify the referred object,
not just the reference. Perhaps you meant that, but I am not sure, and
simply trying to clarify.

V
 
P

Phlip

Pierre said:
But what is the difference between :

The sample you wrote invokes the Return Value Optimization, so there's very
little difference.

References to constant temporaries are useful as arguments:

void foo(std::string const & bar);
...
foo("yo mamma");

Now the string literal will copy-construct a temporary std::string, and bar
will bind to this. And if you pass a real string into foo(), then bar will
efficiently bind to that. foo() is easy to program, without regard to
efficiency.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top