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;
} 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.
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;
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.
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
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
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.
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!
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
...
|
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,...
|
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...
|
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++...
| |
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...
|
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...
|
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;
|
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)
?
|
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...
|
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: 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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |