Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About const references

Reply
Thread Tools

About const references

 
 
vineoff
Guest
Posts: n/a
 
      01-27-2006
When you should not or can't use const references?

Like

void f ( const Foo& b) ;

or

int main() { const int& r = 5; }

 
Reply With Quote
 
 
 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      01-27-2006

vineoff wrote:
> When you should not or can't use const references?
>
> Like
>
> void f ( const Foo& b) ;


Use a const reference when you need access to data or query methods in
the object but won't be "changing" that object. By using a const
reference you are saying, "I am not going to change this object." Of
course you can violate that by const_cast and the object can have
"mutable" variables that can change in a const reference. It is a
contract that you make with clients of your class or method.

It also allows constant references to be passed in where otherwise they
can't. But this is really a concequence of the contract made by the
const keyword...you can't use anything on that object that could change
the object so you can't use non-const methods or pass it to a function
that would cause the constness of the object to go away.

>
> or
>
> int main() { const int& r = 5; }


That doesn't look legal.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-27-2006
vineoff wrote:
> When you should not or can't use const references?


You should use references as arguments most of the time, unless there is
a compelling reason to use a pointer or pass by value.

You should use references as members when the lifetime of the owner of
that member does not exceed (ever) the lifetime of the referred object and
the association is never broken (or changed) during the lifetime of the
owner object. And of course, we assume no containment here, just the
association.

You should use references as return value types so that function calls
could be chained or the result could be used in an expression.

Now, as to 'const', you should always qualify your objects 'const' if you
don't expect to change them.

Now, combine the negative of those statements and you will get the reasons
_not_ to use references to const. For example, if in a situation calling
for *association* between objects you expect to break that association at
some moment during the lifetime of the *referring* object, then you should
not use a reference at all, and should use a pointer.

> Like
>
> void f ( const Foo& b) ;


This is a very common idiom.

> or
>
> int main() { const int& r = 5; }


This use of references while valid, is not warranted. You should simply
write

int main() { const int r = 5; }

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
const references -- how to document intent of const qualification Andrew R. Thomas-Cramer Java 8 07-11-2003 05:01 PM



Advertisments