Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > My overlaoded assignment operator doesn't work

Reply
Thread Tools

My overlaoded assignment operator doesn't work

 
 
heng
Guest
Posts: n/a
 
      12-04-2006
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;
}

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-04-2006
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.

 
Reply With Quote
 
 
 
 
heng
Guest
Posts: n/a
 
      12-04-2006
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;

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-04-2006
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.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-04-2006
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
 
Reply With Quote
 
heng
Guest
Posts: n/a
 
      12-04-2006
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


 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      12-04-2006
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.
 
Reply With Quote
 
TvN
Guest
Posts: n/a
 
      12-05-2006
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!

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-05-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
What the assignment operator from other type(with template) doesn't work? PengYu.UT@gmail.com C++ 4 11-09-2005 03:47 AM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments