473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reference mebmber variable

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
4 1655
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
4
by: =?Utf-8?B?QWxla3MgS2xleW4=?= | last post by:
When I call sub in VB I can sent value of variable or reference to this value. The question is. Suppose I have class public class AA .... end class and declare object of this class dim objAA as...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.