Returning const reference value from functions

S

Sree

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.
 
K

Karl Heinz Buchegger

Sree said:
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.
 
R

Richard Herring

Sree said:
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
?
 
J

JKop

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top