473,796 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloaded operators returning wrong values

i have overlaoded all of my arithmetic operators but all are
functioning as multiplication. below is a sample of the addition
operator:

Rational operator + (const Rational& r1, const Rational& r2){
//Postcondition: sum of r1 and r2 are returned

int numerator;
int denominator;

denominator = (r1.getDen() * r2.getDen());
numerator = (r1.getNum() * r2.getDen() + r2.getNum() *
r1.getDen());
Rational sum (numerator, denominator);

return sum;
}

Mar 28 '06
10 1865
Axter wrote:

[snip]
I've reread you're previous post on this subject, but I don't see where
you're giving a valid reason why Herb Sutters recommend method should
not be recommended as a general rule.
Right. I just gave examples where it might be better/easier to go the other
way. Given the closing remark of your current posting, I see now that I was
reading too much into the recommendation. Sorry.

However, in the course of this conversation, I have been thinking about this
issue more carefully; and now I feel prepared to actually provide a reason
why the general recommendation should be to define operator@= in terms of
operator@ and operator=. I think it is less error prone than the
Sutter/Alexandrescu way.

The example of the OP can be used to illustrate this. Let us have a look at
a rational number class:

class rational {

long n; // numerator
long d; // denominator

public:

// ... stuff

};

Ignoring reduction to lowest possible terms, the Sutter/Alexandrescu way to
go about addition is:

rational & operator+= ( rational & lhs, rational const & rhs ) {
lhs.n = lhs.n * rhs.d + lhs.d * rhs.n;
lhs.d *= rhs.d;
return ( lhs );
}

rational operator+ ( rational const & lhs, rational const & rhs ) {
rational result ( lhs );
result += rhs;
return ( result );
}
This is easy to get wrong:

rational & operator+= ( rational & lhs, rational const & rhs ) {
lhs.d *= rhs.d;
lhs.n = lhs.n * rhs.d + lhs.d * rhs.n; // wrong: uses new lhs.d !
}

The archives show posts dealing with re-implementations of std::complex that
suffer from this kind of bug. And I myself found myself recently falling
for this while working on a matrix class (maybe that is why I am so
sensitive to this issue right now): I was using entries that I had just
overwritten.

On the other hand,

rational operator+ ( rational const & lhs, rational const & rhs ) {
rational result ( lhs.n * rhs.d + lhs.d * rhs.n, lhs.d * rhs.d );
return result;
}

rational & operator+= ( rational & lhs, rational const & rhs ) {
lhs = lhs + rhs;
return ( lhs );
}

is much less prone to this kind of bug. (Come to think of it, this might be
the reason that Sutter and Alexandrescu mention the complex number class as
a case where they would recommend the second approach.)

Your followup post suggested the opposite approach using swap method.
Actually, I wrote that in my first post on this topic. In my follow up, I
elaborated on expression templates.
But not all classes have swap method, and infact, most don't. Those
that do, don't always have an efficient swap method.
True, in that case, you would use assignment. I suggested the swap method
because of the examples that I mentioned: matrix multiplication and
arbitrary precission operations. In both cases, a swap method *should* be
there and more efficient than assignment.

I'm sure there are times when using the opposite approach would be more
efficient, but in general, I would recommend Herb Sutters method.
As far as efficiency is concerned, you are right. However, I would remark
that this optimizes operator@= only: operator@ will not get any faster than
a direct implementation. Also, I would contend that expression templates
are yet more efficient to eliminate unwanted temporaries (altough the added
complexity to the code is considerable and for this reason I would not
recommend this as a general approach). Anyway, as a measure to optimize
performance, I would agree with you.

However, as a general guideline I would rather recommend the following
procedure:

1) First, write operator@= in terms of operator@ and operator= because
that is the most easy version to get right.
2) If profiling shows a need for optimization, refactor operator@=. Use
your operator@ code for inspiration.
3) Run tests to check whether the semantics agree. This should catch
bugs like the one above.
4) Finally rewrite operator@ following the Sutter/Alexandrescu approach.
This eliminates code dubblication.

The Sutter/Alexandrescu recommendation, to me, looks a little bit like
premature optimization.

Remember, that a general rule is for general purposes. It doesn't mean
that the rule is required to be followed for every requirement.


Thanks for reminding me. I tend to forget about caveats that always apply.
Best regards

Kai-Uwe Bux
Apr 8 '06 #11

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

Similar topics

5
6889
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
20
37402
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The C++ Programming Language_, section 11.2 (page 263), these three operators 'take a name, rather than a value, as their second operand and provide the primary means of referring to members. Allowing them to be overloaded would lead to subtleties.'"...
1
2227
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
10
1989
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now my doubt is , if u have something like p->a ....where p(say) is a pointer of a user defined type, then u can overload this because p is determined at runtime ???is this right ??? also if "a" is an object of the some user defined class,
14
3662
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast , reinterpret_cast . Theremust be some reason for this restriction for each of the
5
5102
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler complains. class myclass
13
2008
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base) depending on the operator: class Derived : public Base { };
5
2294
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
18
2644
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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 we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.