constant references?

R

Rickard Andersson

What it the use of constant references in methods and functions? Is it a
design issue or hwat?

A reference that cannot be edited? huh?
Why pass a reference then?
 
?

=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

Rickard said:
What it the use of constant references in methods and functions? Is it a
design issue or hwat?

A reference that cannot be edited? huh?
Why pass a reference then?

At least one example of when constant references are useful is the case
when you want to pass an object as a read only parameter to some function:


class A
{
// add members here
}

void foo(const A& a)
{
// access a thru read only operations
}

By declaring the parameter 'a' as a const refernce you tell users of
foo() that their object won't be altered as a result of the call to
foo(). You also enable clever compilers to make assumptions on how 'a'
will be used inside foo() and based on that they might be able to do
some optimisations when compiling your code. Last but not least, if the
size of A should large you will avoid passing the whole object and
instead pass a suposedly much smaller reference.

:.:: mattias
 
R

Rickard Andersson

Okay, how do I know when I should pass a const reference and not a simple
const?

Dummy question, but it's confusing to me!

btw, you're swedish living in Luleå? :)
 
?

=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

Rickard said:
Okay, how do I know when I should pass a const reference and not a simple
const?

I guess you mean when you should one of the following

(1) void foo(const A& a);

(2) void foo(const A a);

My short answer to this would be: it makes little sense to do something
like (2). Since you are passing your A object by value the variable you
pass to foo() will be copied and there will be now way for foo() to
alter it. Declaring 'foo(const A a)' doesn't tell users of foo()
anything more than if you would declare it 'foo(A a)'.

:.:: mattias

PS.
btw, you're swedish living in Luleå?
I'm swedish and living in Gothenburg. I used to study in Luleå.
 
T

tom_usenet

What it the use of constant references in methods and functions? Is it a
design issue or hwat?

A reference that cannot be edited? huh?
Why pass a reference then?

If you pass by value, a copy of the thing you are passing will be
made. Some objects either aren't copyable or are inefficient to copy,
so you pass them by const reference (or pointer). Finally, if you want
polymorphism, you have to use pass by reference otherwise you get
slicing. e.g.

void foo(const Base& b);

foo(aDerived);

Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
R

Rolf Magnus

Rickard said:
Okay, how do I know when I should pass a const reference and not a
simple const?

Well, a "simple const" as parameter isn't very useful. A function taking
an argument by value cannot modify that argument anyway, since the
function receives a copy. With some class types, a copy can easily get
quite expensive, and that's when you use call-by-reference instead of
call-by-value. And if the function doesn't modify the object, you use a
const reference. I think many people use call-by-value for builtin
small types and call-by-(const-)reference for most class types.
 
J

jeffc

Rickard Andersson said:
What it the use of constant references in methods and functions? Is it a
design issue or hwat?

A reference that cannot be edited? huh?
Why pass a reference then?

What is the alternative? Pass by value. When you pass by value, you make a
copy. If the object is tiny, it's no big deal. But some objects are not
tiny.
 
E

E. Robert Tisdale

Mattias said:
Rickard said:
Okay, how do I know when I should [implement a function]
to pass a const reference and not a simple const?

I guess you mean when you should one of the following

(1) void foo(const A& a);

(2) void foo(const A a);

The second declaration is equivalent to

void foo(A a);

Identifier a is the *formal* argument.
When you invoke foo:

A a;
foo(a);

identifier a is the *actual* argument.
The compiler emits code to pass a by reference in the first case and
the compiler emits code to pass a by value in the second case.
It does *not* matter whether a is passed by value or const reference
as far as the program that invokes foo is concerned.
The only difference is that pass by reference doesn't require
the copy of the actual argument like pass by value.
This is an important optimization
only when a is larger than about one or two machine words.
My short answer to this would be:
it makes little sense to do something like (2).
Since you are passing your A object by value,
the [actual argument] you pass to foo() will be copied

[into the formal argument]
and there will be now way for foo() to alter [the actual argument].
Declaring 'foo(const A a)' doesn't tell users of foo()
anything more than if you would declare it 'foo(A a)'.

Using the const qualifier for formal arguments passed by value
is only useful in the function *definition*:

void foo(const A a) {
// The const qualifier informs the compiler that
// any attempt to modify a in the body of the function
// is a mistake and the compiler should issue a warning.
}
 
J

jeffc

E. Robert Tisdale said:
The second declaration is equivalent to

void foo(A a);

Not really. If the definition is

void foo(const A a)
{
a = 3;
}

then there will be a compile error for (2).
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top