Returning const reference value from functions



 DEVELOP > c-Plus-Plus > Returning const reference value from functions

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Sree"
Date: 24 Jun 2004 01:26:13 PM
Object: 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/
.

User: "Richard Herring"

Title: Re: Returning const reference value from functions 25 Jun 2004 05:56:45 AM
In message <2004624-202613-915365@foorum.com>, Sree
<no_spam_please@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
.

User: "JKop"

Title: Re: Returning const reference value from functions 25 Jun 2004 06:50:30 AM
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
.

User: "Karl Heinz Buchegger"

Title: Re: Returning const reference value from functions 25 Jun 2004 05:54:05 AM
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
kbuchegg@gascad.at
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER