473,385 Members | 1,927 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,385 software developers and data experts.

Returning const reference value from functions


Hello All,
When will it be benefical to return a value from a function as a const reference
?

Say I have classes like this

class B {
public:
int someValue1;
int someValue2;
void ChangeValue();
void ConstFunc() const;
};

void B::ChangeValue()
{
int tmp = someValue1;
someValue1 = someValue2;
someValue2 = tmp;
}
class A {
public :
int value;
void inspect(const B& b);
const B& returnConstRef();
private :
int someInt;
B classB;
};

const B& A::returnConstRef()
{
return classB;
}

In the above class, returnConstRef returns a const reference to B. So whatever
we get like this should not be able to change class B like below. Also the
compiler should not allow me to assign a non-const class B (classB) to classA.
returnConstRef().

A classA;
B classB = classA.returnConstRef();
classB.ChangeValue();

With the above code there is no problem with the compiler. I am wrong some where
in my understanding of the returning values by reference, Can somebody please
correct ?

Thanks in advance
KInd.
--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
Jul 22 '05 #1
3 2119
Sree wrote:

Hello All,
When will it be benefical to return a value from a function as a const reference
?
When the cost of returning by value is to high due to copying of the
return value.
With builtin types like int, double, char, ... this is practically never
the case. User defined types need to have a closer inspection.

[snip]
Also the
compiler should not allow me to assign a non-const class B (classB) to classA.
returnConstRef().
Maybe I read something different to what you intended. But: Why should
the compiler not allow this?

A classA;
B classB = classA.returnConstRef();
classB.ChangeValue();


That does something completely different.
A new B object is constructed, which is initialized from the
B object in A.
After that classB and the B object in classA have nothing in common.
This would not change, if you didn't return a const reference, but
a non const instead.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
In message <20*******************@foorum.com>, Sree
<no************@f-m.fm> writes

Hello All,
When will it be benefical to return a value from a function as a const
reference
?
See the thread "Read-only, as opposed to const member" for a lot more
debate on this issue ;-)

Say I have classes like this

class B {
public:
int someValue1;
int someValue2;
void ChangeValue();
void ConstFunc() const;
};

void B::ChangeValue()
{
int tmp = someValue1;
someValue1 = someValue2;
someValue2 = tmp;
}
class A {
public :
int value;
void inspect(const B& b);
const B& returnConstRef();
private :
int someInt;
B classB;
};

const B& A::returnConstRef()
{
return classB;
}

In the above class, returnConstRef returns a const reference to B. So whatever
we get like this should not be able to change class B like below. Also the
compiler should not allow me to assign a non-const class B (classB) to classA.
returnConstRef().

A classA;
B classB = classA.returnConstRef();
This constructs a new B which is a *copy* of the reference returned by
returnConstRef(). The new B object has no connection with the one in
classA.
classB.ChangeValue();
And this can therefore modify the new copy.

Did you perhaps mean to write

B const & classB = classA.returnConstRef();
// now classB is a (const) reference to classA.classB
classB.ChangeValue(); // this won't compile
?
With the above code there is no problem with the compiler. I am wrong
some where
in my understanding of the returning values by reference, Can somebody please
correct ?


--
Richard Herring
Jul 22 '05 #3
Sree posted:
In the above class, returnConstRef returns a const reference to B. So
whatever we get like this should not be able to change class B like
below.


Wouldn't it be nice if you could make the B object read-only.

class A
{
read-only B objectB;
};
I've written another thread about this entitled "Read only, as opposed to
const member variable". Don't look at my original post, but my second one in
the thread. Essentially it does this:

int main(void)
{
A a;

TakesB(a.objectB);

a.objectB = 4; //Compile ERROR

//Essentially, it is read-only
}
-JKop
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
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...
4
by: tech | last post by:
Hi, I want to return a reference to member object provided in class Test, so have provided a getter function GetObj() and declared it const. However i'm getting a compiler error that i need to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.