473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

returning const references/pointers from functions?

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
1 1233
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
3
by: Alfonso Morra | last post by:
I'm in the process of implementing an abstract factory design pattern for an application. I would like to know, which is the recommended way of returning objects from the factory - by reference or...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.