reference mebmber variable

S

siddhu

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

Ian Collins

siddhu said:
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!
 
A

Abhishek Padmanabh

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

Tadeusz B. Kopec

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

James Kanze

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

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top