By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

reference mebmber variable

P: n/a
If there is reference member variable in the class, why doesn't
default assignment operator work?

class A
{
int& i;
public:
A( int& ii):i(ii){}
//A& operator=(const A& a){i = a.i;}
};

int main()
{
int k =10;
A a(k);
A b(k);
a = b; //This does not compile
}

But if I uncomment my assignment operator it works. I hope default
assignment operator works the same way.
Dec 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
siddhu wrote:
If there is reference member variable in the class, why doesn't
default assignment operator work?

class A
{
int& i;
public:
A( int& ii):i(ii){}
//A& operator=(const A& a){i = a.i;}
};

int main()
{
int k =10;
A a(k);
A b(k);
a = b; //This does not compile
}

But if I uncomment my assignment operator it works. I hope default
assignment operator works the same way.
Does your compiler give you a meaningful error message? It should!

--
Ian Collins.
Dec 16 '07 #2

P: n/a
On Dec 16, 9:06 am, siddhu <siddharth....@gmail.comwrote:
If there is reference member variable in the class, why doesn't
default assignment operator work?

class A
{
int& i;
public:
A( int& ii):i(ii){}
//A& operator=(const A& a){i = a.i;}

};

int main()
{
int k =10;
A a(k);
A b(k);
a = b; //This does not compile

}

But if I uncomment my assignment operator it works. I hope default
assignment operator works the same way.
References are non-copyable/non-assignable. Your assignment operator
works because you are not actually assigning one reference to another
reference. The reference member 'i' in your case keeps referring to
the same variable that it referred to when the object was constructed
i.e. 'k'. And you are just assigning the value to it. As a result the
value of 'k' will change upon assignment a=b;. You can verify using
two different variables (having different initial values) to construct
A objects a and b.

I don't know what does the compiler generated assignment operator do,
but it might just be giving the error seeing a (non-assignable)
reference member. I am not sure how one would write a statement in C++
which would do assignment of references because anything like this:

reference_1 = reference_2;

would not actually be assigning a reference to a reference but
assigning variable referred to by reference_1 by value of variable
referred to by reference_2.
Dec 16 '07 #3

P: n/a
On Sat, 15 Dec 2007 21:27:08 -0800, Abhishek Padmanabh wrote:
On Dec 16, 9:06 am, siddhu <siddharth....@gmail.comwrote:
>If there is reference member variable in the class, why doesn't default
assignment operator work?

class A
{
int& i;
public:
A( int& ii):i(ii){}
//A& operator=(const A& a){i = a.i;}

};

int main()
{
int k =10;
A a(k);
A b(k);
a = b; //This does not compile

}

But if I uncomment my assignment operator it works. I hope default
assignment operator works the same way.
[snip]
I don't know what does the compiler generated assignment operator do,
but it might just be giving the error seeing a (non-assignable)
reference member. I am not sure how one would write a statement in C++
which would do assignment of references because anything like this:

reference_1 = reference_2;

would not actually be assigning a reference to a reference but assigning
variable referred to by reference_1 by value of variable referred to by
reference_2.
If an object contains a reference copy constructor and assignment won't
be generated by compiler so asking what they do makes no sense - they
simply don't exist. And references cannot be reseated.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
Mr. Cole's Axiom:
The sum of the intelligence on the planet is a constant;
the population is growing.
Dec 16 '07 #4

P: n/a
On Dec 16, 8:28 pm, "Tadeusz B. Kopec" <tko...@NOSPAMPLEASElife.pl>
wrote:
On Sat, 15 Dec 2007 21:27:08 -0800, Abhishek Padmanabh wrote:
On Dec 16, 9:06 am, siddhu <siddharth....@gmail.comwrote:
If there is reference member variable in the class, why
doesn't default assignment operator work?
class A
{
int& i;
public:
A( int& ii):i(ii){}
//A& operator=(const A& a){i = a.i;}
};
int main()
{
int k =10;
A a(k);
A b(k);
a = b; //This does not compile
}
But if I uncomment my assignment operator it works. I hope
default assignment operator works the same way.
[snip]
I don't know what does the compiler generated assignment
operator do,
It doesn't. If the class contains a reference, the compiler
generates a declaration for the assignment operator (if the user
doesn't provide one), but it is an error (requiring a
diagnostic) if the compiler is required to generate the
definition (because the operator was actually used).
but it might just be giving the error seeing a
(non-assignable) reference member. I am not sure how one
would write a statement in C++ which would do assignment of
references because anything like this:
reference_1 = reference_2;
would not actually be assigning a reference to a reference
but assigning variable referred to by reference_1 by value
of variable referred to by reference_2.
If an object contains a reference copy constructor and
assignment won't be generated by compiler so asking what they
do makes no sense - they simply don't exist. And references
cannot be reseated.
There's no problem with the copy constructor, just assignment.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.