473,486 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

operator overloading q'n

Hi there. Suppose we have (in C):

if (x != y)
x = y;

it's obvious the conditional is unnecessary, and a C programmer would
frown upon this code most likely coming from a newbie who is somehow
convinced that the 'if' part makes his code faster by avoiding the more
"expensive" assignment. But this code *could* actually be faster in
C++, when dealing with classes and you never know what happens under the
hood (theoretically you *could* know this provided there's source code
and/or docs handy). How do you guys deal with this kind of stuff? Do the
authors of modern libraries avoid overloaded operators (even if they
make sense, like in std::string)? It's all to easy to forget that almost
any operator could actually be a function call, and the only way to keep
not forgetting it is to call the method explicitly (i.e. if
(foo.operator==(bar))). I could be wrong, please share your opinion ;)

Jul 22 '05 #1
1 993
It's taken care of "under the hood," as you say. If the test for
equality is worthwhile, it will be done inside the assignment operator,
possibly circumventing the more expensive part of the assignment.

T const& T::operator = ( T const& other )
{
if( *this != other )
{
// Do expensive copying and stuff.
}

return *this;
}

rihad wrote:
Hi there. Suppose we have (in C):

if (x != y)
x = y;

it's obvious the conditional is unnecessary, and a C programmer would
frown upon this code most likely coming from a newbie who is somehow
convinced that the 'if' part makes his code faster by avoiding the more
"expensive" assignment. But this code *could* actually be faster in
C++, when dealing with classes and you never know what happens under the
hood (theoretically you *could* know this provided there's source code
and/or docs handy). How do you guys deal with this kind of stuff? Do the
authors of modern libraries avoid overloaded operators (even if they
make sense, like in std::string)? It's all to easy to forget that almost
any operator could actually be a function call, and the only way to keep
not forgetting it is to call the method explicitly (i.e. if
(foo.operator==(bar))). I could be wrong, please share your opinion ;)


Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2558
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
6402
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
3050
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
2042
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...
67
8571
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
2265
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
3602
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
3248
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
3471
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...
8
2948
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
6964
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
7126
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,...
1
6842
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...
0
7330
tracyyun
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...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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.