const references : confusion

R

raj

I need explanation of the behavior of the following code. When run as it is, it gives error.

//-------------------------<start>------------------
#include <iostream.h>
#include <stdlib.h>

class ABC
{
private: int x;

public:
ABC() { x=0; }
ABC (int a) { x=a; }
ABC (const ABC& abc)
{
x=abc.x+1; //increasin by 1 just as a token of copy constructor invocation
cout<<"\n\tCopy constructor invoked.";
}

ABC& operator=(ABC& abc) //NO ERROR IF CHANGED TO : ABC& operator=(const ABC& abc)
{ //line 18
x=abc.x;
return (*this);
}
ABC friend frnd_func();
};

ABC frnd_func()
{
ABC abc(100);
return abc;
}

int main()
{
ABC a1(5);
ABC a2=a1; //just to test copy constructor.
a2=frnd_func(); //gives error : "initialisation of non-const reference type 'cxlass ABC&'
// from rvalue of typpe 'ABC' in passing argument 1 of 'ABC::eek:perator=(ABC&)'
cout<<"\n\n";system("PAUSE");
return 0;
}
//-------------------------<end>---------------------

Running it in Bloodshed Dev C++ gives the following error:
35: Initialisation of non-const reference type 'class ABC&'
35: from rvalue of type 'ABC'
18: in assigning argument 1 of 'ABC::eek:perator=(ABC &)'

But, if I change the = overloading declaration to:
ABC& operator=(const ABC& abc)
then the error vanishes.

Why should i need to add the 'const' keyword here?
 
Q

ququqa

In expression:
a2=frnd_func();
frnd_funct() creates a temporary object ABC that is assigned to a2.
All temporary objects are const (you cannot change them), so the
operator: ABC& operator=(ABC& abc) has to take a const reference to
assign temporary object.

regareds
q

http://members.lycos.co.uk/ququqa2/
 
J

John Harrison

ququqa said:
In expression:
a2=frnd_func();
frnd_funct() creates a temporary object ABC that is assigned to a2.
All temporary objects are const (you cannot change them),

That is not true, for instance

X func();

X x;
func() = x;

is perfectly legal code (assuming X is a class, and assignment of X is
legal). The return value from func is a temporary, but it is not const.

The correct rule is that you cannot bind a non-const reference to a
temporary object.

This subject gets a lot of coverage in this group because it seems a lot of
people are surprised by this rule.

john
 
J

John Harrison

Running it in Bloodshed Dev C++ gives the following error:
35: Initialisation of non-const reference type 'class ABC&'
35: from rvalue of type 'ABC'
18: in assigning argument 1 of 'ABC::eek:perator=(ABC &)'

But, if I change the = overloading declaration to:
ABC& operator=(const ABC& abc)
then the error vanishes.

Why should i need to add the 'const' keyword here?

Rules of C++, you cannot bind a temporary object to a non-const reference.
The return value of a function is a temporary.

john
 
S

Sharad Kala

John Harrison said:
Rules of C++, you cannot bind a temporary object to a non-const reference.
The return value of a function is a temporary.
True and it's good that your compiler has pointed it out to you.
There are compilers (with options) which as a language extension allow binding
temporary objects
to non-const references. So if such an option is set/unset you may just write
non-standard code without even realizing it.
E.g. On VC++ compile without /Za compiler option your code would run just fine.

-Sharad
 
R

raj

(tho i dont undestand why the compiler shd take such pains to ensure the constness of temporary objects... particularly, whn i'm takin control of them using copy constructors)
 
J

John Harrison

raj said:
(tho i dont undestand why the compiler shd take such pains to ensure the
constness of temporary objects... particularly, whn i'm takin control of
them using copy constructors)
Jesus, how many times does this have to be said.

Temporary objects are not const, the compiler does not enforce the constness
of temporary objects.

There is a recent thread 'non-const function return values: gcc bug or
language flaw' which goes into this in great detail and concludes with the
real reason for this rule, contributed by the great man himself (Bjarne
Stroustrup).

john
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top