My overlaoded assignment operator doesn't work


H

heng

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;
}
 
Ad

Advertisements

R

Rolf Magnus

heng said:
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.
 
H

heng

Rolf said:
This program never calls operator=. Note that


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;
 
R

Rolf Magnus

heng said:
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.
 
K

Kai-Uwe Bux

heng said:
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
 
Ad

Advertisements

J

Jacek Dziedzic

heng said:
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.
 
T

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!
 
Ad

Advertisements

R

red floyd

TvN said:
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.
 

Top