473,511 Members | 16,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My overlaoded assignment operator doesn't work

I define my own assignment operator, but it doesn't work as I imagined.

#include<iostream>
#include <string>
using namespace std;

class A{
int x;
public:
int y;
int z;
A& operator=(const A& t)
{
z=t.y;
y=t.z;
return *this;
}
};

int main()
{
A aclass;
aclass.y=9;
aclass.z=11;
A bclass=aclass;
cout<<aclass.z<<endl<<bclass.y;
string str;
cin>>str;
return 0;
}

Dec 4 '06 #1
8 1652
heng wrote:
I define my own assignment operator, but it doesn't work as I imagined.
That's unfortunate. What did you imagine, and what does it do instead?
#include<iostream>
#include <string>
using namespace std;

class A{
int x;
public:
int y;
int z;
A& operator=(const A& t)
{
z=t.y;
y=t.z;
return *this;
}
};

int main()
{
A aclass;
aclass.y=9;
aclass.z=11;
A bclass=aclass;
cout<<aclass.z<<endl<<bclass.y;
string str;
cin>>str;
return 0;
}
This program never calls operator=. Note that
A bclass=aclass;
is not assigment, but initialization.

Dec 4 '06 #2
Rolf Magnus wrote:
This program never calls operator=. Note that
A bclass=aclass;

is not assigment, but initialization.
Thanks. How may I call operator=? Could you please show me an example?

And I am kind of confused about assignment and initialization. Is the
following belonging to assignment?

A bclass;
bclass=aclass;

Dec 4 '06 #3
heng wrote:
Rolf Magnus wrote:
>This program never calls operator=. Note that
A bclass=aclass;

is not assigment, but initialization.

Thanks. How may I call operator=? Could you please show me an example?

And I am kind of confused about assignment and initialization.
It's rather simple:

Type Object = OtherObject; <- initialization
Object = OtherObject; <- assignment

In other words: if you define a new object, it's initialization, if you have
an already existing object on the left side, it's assignment.
Is the following belonging to assignment?

A bclass;
bclass=aclass;
Yes.
Dec 4 '06 #4
heng wrote:
I define my own assignment operator, but it doesn't work as I imagined.

#include<iostream>
#include <string>
using namespace std;

class A{
int x;
public:
int y;
int z;
A& operator=(const A& t)
{
z=t.y;
y=t.z;
I would expect:

z = t.z;
y = t.y;

Are you sure you want that?
return *this;
}
};

int main()
{
A aclass;
aclass.y=9;
aclass.z=11;
A bclass=aclass;
This calls the copy constructor.
cout<<aclass.z<<endl<<bclass.y;
string str;
cin>>str;
return 0;
}

Best

Kai-Uwe Bux
Dec 4 '06 #5
Thanks.

Kai-Uwe Bux wrote:
heng wrote:
I define my own assignment operator, but it doesn't work as I imagined.

#include<iostream>
#include <string>
using namespace std;

class A{
int x;
public:
int y;
int z;
A& operator=(const A& t)
{
z=t.y;
y=t.z;

I would expect:

z = t.z;
y = t.y;

Are you sure you want that?
return *this;
}
};

int main()
{
A aclass;
aclass.y=9;
aclass.z=11;
A bclass=aclass;

This calls the copy constructor.
cout<<aclass.z<<endl<<bclass.y;
string str;
cin>>str;
return 0;
}


Best

Kai-Uwe Bux
Dec 4 '06 #6
heng wrote:
I define my own assignment operator, but it doesn't work as I imagined.

#include<iostream>
#include <string>
using namespace std;

class A{
int x;
public:
int y;
int z;
A& operator=(const A& t)
{
z=t.y;
y=t.z;
return *this;
}
};

int main()
{
A aclass;
aclass.y=9;
aclass.z=11;
A bclass=aclass;
cout<<aclass.z<<endl<<bclass.y;
string str;
cin>>str;
return 0;
}
This invokes the copy constructor, not the assignment
operator. Why not define that one too?

HTH,
- J.
Dec 4 '06 #7
TvN
Hi,
I define my own assignment operator, but it doesn't work as I imagined.
[skiped]

People already said why it was not called, my reply about your
operator;) Why do you check for self assigment? What about such
situation:

A a;
....
a = a;

So I think you should always check for this:

A& operator=(const A& t)
{
if (this != &t)
{
....
}
return *this;
}

Regards,
Volodymyr!

Dec 5 '06 #8
TvN wrote:
Hi,
>I define my own assignment operator, but it doesn't work as I imagined.
[skiped]

People already said why it was not called, my reply about your
operator;) Why do you check for self assigment? What about such
situation:

A a;
...
a = a;

So I think you should always check for this:

A& operator=(const A& t)
{
if (this != &t)
{
....
}
return *this;
}
See Sutter's "Exceptional C++" for a discussion of self-assignment
checks. It should only be necessary as an optimization, if you use the
construct-and-swap metaphor.

Dec 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
3197
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
8
17843
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
9
3045
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
17
3716
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how...
7
4659
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
4
1851
by: PengYu.UT | last post by:
The following shows the source code and the error message(from g++-3.3). I wrote two assignment operator. One is for the same type case, the other one is for different type case. I'm wondering why...
5
1445
by: Rob | last post by:
To follow up on the "copy constructor clarification thread"... The assignment operator syntax shown previously: MyClass% operator=(const MyClass%); seems to have problems if you have member...
11
2375
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
34
7134
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
0
7138
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
7418
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...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.