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

returning const references/pointers from functions?

P: n/a
When would you return a const reference/pointer from a function instead
of either the object itself, or a non constreference/pointer? And when
would you need to return a reference or pointer at all, instead of the
object itself?

Sep 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
When would you return a const reference/pointer from a function instead
of either the object itself,
I would return a const reference/pointer to avoid "premature pessimization"
but only if the object is alive even after the function returns.

If the object is non-copyable there is no other way than returning a
reference/pointer.
or a non constreference/pointer?
If the caller should be allowed to modify the object through the returned
reference/pointer.
And when
would you need to return a reference or pointer at all, instead of the
object itself?


You cannot return the object itself. You can either return a
reference/pointer to it, or you can return a copy of it.

When you need to work on a specific object, not a copy of it, you have to
return a reference/pointer.

Ali
--
Plug: ACCU's Silicon Valley Chapter meets on second Tuesdays. The meetings
are open to public and free of charge. Please come tonight for a talk on
Ada:

http://accu-usa.org/index.html
Sep 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.