By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

constant references?

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Rickard Andersson wrote:
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

Jul 22 '05 #2

P: n/a
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å? :-)
"Mattias Brändström" <br****@ludd.luth.se> skrev i meddelandet
news:3f*********************@news.luth.se...
Rickard Andersson wrote:
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

Jul 22 '05 #3

P: n/a
On Tue, 25 Nov 2003 12:42:43 GMT, "Rickard Andersson"
<ri************@telia.com> wrote:
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
Jul 22 '05 #4

P: n/a
Rickard Andersson wrote:
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å.

Jul 22 '05 #5

P: n/a
Rickard Andersson wrote:
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.
Jul 22 '05 #6

P: n/a

"Rickard Andersson" <ri************@telia.com> wrote in message
news:77********************@newsc.telia.net...
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.
Jul 22 '05 #7

P: n/a
Mattias Brändström wrote:
Rickard Andersson wrote:
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.
}

Jul 22 '05 #8

P: n/a

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...

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

(2) void foo(const A a);


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).
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.