469,599 Members | 2,616 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

the use of returning const &

what is the use of returning "const & "from a function in C++ ?? Is it used
somewhere at all ??

thank you,
Ranjan.

Sep 10 '05 #1
1 1237
* maadhuu:
what is the use of returning "const & "from a function in C++ ??
What's with the double question mark?

Anyway, re the question, a reference means this is an object that exists
somewhere (not a copy), and const prevents the client code from calling
non-const member functions or directly modifying data members.

Both are guarantees: the existence guarantee is a guarantee the function gives
the client code, so that no checking of existence is needed, and the constness
is a guarantee the function gives about the object.

Is it used somewhere at all ??


Yes. A simple example is a singleton function returning a reference to a
static object. A perhaps less simple, but more often occurring, example, is
the 'at' or operator[] member function of a container such as std::vector.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 10 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jochen Zeischka | last post: by
4 posts views Thread by Patrick | last post: by
18 posts views Thread by cppaddict | last post: by
16 posts views Thread by Steven T. Hatton | last post: by
8 posts views Thread by Derek | last post: by
4 posts views Thread by scparker | last post: by
23 posts views Thread by pauldepstein | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.