473,509 Members | 3,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading the =Operator

14 New Member
Hello,
I was taking a look at overloading the = operator in a C Plus Plus book. Part of the code of the overloaded =operator included checking for the case when the same object was on both sides of the equal sign. When and why would you have a situation like this?
Expand|Select|Wrap|Line Numbers
  1. someobject1=someobject1;
Thank You
Nov 13 '07 #1
4 1458
Cucumber
90 New Member
Hello,
I was taking a look at overloading the = operator in a C Plus Plus book. Part of the code of the overloaded =operator included checking for the case when the same object was on both sides of the equal sign. When and why would you have a situation like this?
Expand|Select|Wrap|Line Numbers
  1. someobject1=someobject1;
Thank You
yes
thats why that safety code is included in the operator=
Nov 13 '07 #2
UofFprogrammer
14 New Member
I understand the safety code part, but I couldn't think of an intentional situation where the code would have same object on both sides of the assignment operator.
Nov 13 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
When and why would you have a situation like this?

Code: ( text )
someobject1=someobject1;
You can't prevent some nutcase from doing this so you have to code defensively.

This is important where objects with dynamically allocated assets are concerned. The left object has to delete its contents in preparation for a copy of the right object's contents. In this bizarre case the object would have deleted its own contents and then crashed the program trying to make a copy of them.

It's easy enough to avoid the whole mess by just disallowing self-assignment.
Nov 13 '07 #4
UofFprogrammer
14 New Member
You can't prevent some nutcase from doing this so you have to code defensively.

This is important where objects with dynamically allocated assets are concerned. The left object has to delete its contents in preparation for a copy of the right object's contents. In this bizarre case the object would have deleted its own contents and then crashed the program trying to make a copy of them.

It's easy enough to avoid the whole mess by just disallowing self-assignment.
Weaknessforcats, you hit the nail on the head. That was the situation that was in the book with dynamically allocated assets being deleted before being reassigned. I was curious if there was ever a situation where that code would be done intentionally or if it was just programmer error.

Thanks for the response!
Nov 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5213
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
3058
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
2047
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
2472
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
2233
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
3607
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
3253
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
3477
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
4404
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
2951
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
7136
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
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
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
5060
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
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.