472,141 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1595
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Paul Rubin | last post: by
9 posts views Thread by Rick N. Backer | last post: by
7 posts views Thread by Sean | last post: by
5 posts views Thread by Rob | last post: by
11 posts views Thread by anongroupaccount | last post: by
34 posts views Thread by Chris | last post: by
reply views Thread by leo001 | last post: by

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.