473,785 Members | 2,188 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 #1
10 1862
andrew browning wrote :
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;
}


And your problem is ?
This is correct addition of rationals to me.
Mar 28 '06 #2
The following is the recommended approach to overloading arithmetic and
assignment operators:

T& T::operator+=(c onst T&){
//.... impl
return *this
}

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

----------------------------------------------------------------------------------------
David Maisonave
http://axter.com

Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------

Mar 29 '06 #3
Axter wrote:
The following is the recommended approach to overloading arithmetic and
assignment operators:

T& T::operator+=(c onst T&){
//.... impl
return *this
}

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


Recommended for which reason?

I would like to see the "recommende d" approach carried out for matrix
multiplication or arbitrary precission integer multiplication. In both
cases, an in-place implementation is far from obvious (if possible); and
implementing * in terms of *= for matrices or BigInt is likely to create
more temporaries internally than implementing *= in terms of *.

Assuming there is an efficient swap method (like for std::vector), what is
wrong with:

T operator+ ( T const & a, T const & b ) {
// impl ...
return ( result );
}

T & operator+= ( T const & t ) {
T temp = *this + t;
swap( *this, temp );
return ( *this );
}

Also note that, if profiling shows the need for using expression templates,
an implementation of *= in terms of * is more natural that the other way
around -- in fact, I do not see how I would write an expression template
for * in terms of *=; but that could be a lack of imagination on my part.
Best

Kai-Uwe Bux
Mar 29 '06 #4
* Kai-Uwe Bux:
Axter wrote:
The following is the recommended approach to overloading arithmetic and
assignment operators:

T& T::operator+=(c onst T&){
//.... impl
return *this
}

T operator+(const T& lhs, const T& rhs){
T temp(lhs);
return temp += rhs;
}
Recommended for which reason?


Axter's example shows two independent guidelines:

1) Implement operator+ in terms of operator+=.

2) Implement operator+ as a non-member function.

(1) is a somewhat contested guideline. E.g., I seem to recall that
Microsoft has published the exact opposite guideline, and here you are
also arguing against it. As I see it, using (1) you can do no worse
than the opposite approach, and will mostly do better (allowing
operator+= to be as efficient as possible with no temporary involved,
where possible).

(2) has a simple rationale: to treat both arguments on an equal footing.
For example, to allow the same implicit conversions.

I would like to see the "recommende d" approach carried out for matrix
multiplication or arbitrary precission integer multiplication. In both
cases, an in-place implementation is far from obvious (if possible); and
implementing * in terms of *= for matrices or BigInt is likely to create
more temporaries internally than implementing *= in terms of *.
Yes, there are cases where you can't do in-place operations, but how can
you get fewer temporaries by implementing operator+= in terms of
operator+? As I see it, for operator+= you can take advantage of access
to internals, in particular reusing an outer encapsulation or already
allocated internal memory. That seems to me to generally imply fewer
temporaries and such.

Assuming there is an efficient swap method (like for std::vector), what is
wrong with:

T operator+ ( T const & a, T const & b ) {
// impl ...
return ( result );
}

T & operator+= ( T const & t ) {
T temp = *this + t;
swap( *this, temp );
return ( *this );
}
Mostly, what's "wrong" is that this approach /can't/ guarantee an
efficient in-place addition, when type T is such that that could
otherwise be done. If client code has "T x, y; ...; x += y;" then only
the compiler's optimizations can turn that into an in-place addition.
I'm thinking here of client code like

T o;
for( hee; haa; hoo )
{
o += something;
}
Also note that, if profiling shows the need for using expression templates,
an implementation of *= in terms of * is more natural that the other way
around -- in fact, I do not see how I would write an expression template
for * in terms of *=; but that could be a lack of imagination on my part.


I'm not sure, it may be that you have a good point why expression
templates are simply different, but consider

template< typename T >
struct Multiply
{
static inline T apply( T const& a, T const& b )
{
T result( a );
return (result += b);
}
};

as a kind of generic implementation, and then e.g. for double, if
necessary for efficiency,

template<> struct Multiply<double >
{
static inline double apply( double a, double b ) { return a*b; }
};

and so on (disclaimer: I haven't tried this!).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 29 '06 #5
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
Axter wrote:
The following is the recommended approach to overloading arithmetic and
assignment operators:

T& T::operator+=(c onst T&){
//.... impl
return *this
}

T operator+(const T& lhs, const T& rhs){
T temp(lhs);
return temp += rhs;
}
Recommended for which reason?


Axter's example shows two independent guidelines:

1) Implement operator+ in terms of operator+=.

2) Implement operator+ as a non-member function.

(1) is a somewhat contested guideline. E.g., I seem to recall that
Microsoft has published the exact opposite guideline, and here you are
also arguing against it. As I see it, using (1) you can do no worse
than the opposite approach, and will mostly do better (allowing
operator+= to be as efficient as possible with no temporary involved,
where possible).


That works if (and I would contest only if) you can implement operator *=
in-place. I am not arguing against using that idiom where it works. I am
arguing against recommending this as a general rule.


(2) has a simple rationale: to treat both arguments on an equal footing.
For example, to allow the same implicit conversions.
Ok.
I would like to see the "recommende d" approach carried out for matrix
multiplication or arbitrary precission integer multiplication. In both
cases, an in-place implementation is far from obvious (if possible); and
implementing * in terms of *= for matrices or BigInt is likely to create
more temporaries internally than implementing *= in terms of *.


Yes, there are cases where you can't do in-place operations, but how can
you get fewer temporaries by implementing operator+= in terms of
operator+? As I see it, for operator+= you can take advantage of access
to internals, in particular reusing an outer encapsulation or already
allocated internal memory. That seems to me to generally imply fewer
temporaries and such.


In matrix multiplication, you cannot overwrite the coefficients because you
still need them. Thus, you will end up allocating a temporary matrix for *=
anyway. If then, on top of this, you implement * in terms of *=, you may
end up with more temporaries.

[snip]

Also note that, if profiling shows the need for using expression
templates, an implementation of *= in terms of * is more natural that the
other way around -- in fact, I do not see how I would write an expression
template for * in terms of *=; but that could be a lack of imagination on
my part.


I'm not sure, it may be that you have a good point why expression
templates are simply different, but consider

template< typename T >
struct Multiply
{
static inline T apply( T const& a, T const& b )
{
T result( a );
return (result += b);
}
};

as a kind of generic implementation, and then e.g. for double, if
necessary for efficiency,

template<> struct Multiply<double >
{
static inline double apply( double a, double b ) { return a*b; }
};

and so on (disclaimer: I haven't tried this!).

Probably, I need to me more specific on the expressions template business.
Here is a simple expression template implementation for vector addition --
something that I would implement using += as the primitive (since it can be
done in place) and then defining + in terms of +=. However, with expression
templates, it appears that the other way around is more natural:
#include <cstddef>
#include <iostream>

std::size_t const length = 4;
typedef double Number;

class VectorStoragePo licy {

Number data [length];

public:

VectorStoragePo licy ( Number e = 0 )
{
for ( std::size_t i = 0; i < length; ++i ) {
data[i] = e;
}
}

VectorStoragePo licy ( VectorStoragePo licy const & other )
{
for ( std::size_t i = 0; i < length; ++i ) {
data[i] = other[i];
}
}

Number operator[] ( std::size_t i ) const {
return ( data[i] );
}

Number & operator[] ( std::size_t i ) {
return ( data[i] );
}
};
template < typename ExprA, typename ExprB >
class VectorPlusVecto r {

ExprA a_ref;
ExprB b_ref;

public:

VectorPlusVecto r ( ExprA const & a, ExprB const & b )
: a_ref ( a )
, b_ref ( b )
{}

Number operator[] ( std::size_t i ) const {
return ( a_ref[i] + b_ref[i] );
}

};

template < typename Expr >
struct VectorTag : public Expr {

VectorTag ( void )
: Expr()
{}

template < typename A >
VectorTag ( A const & a )
: Expr( a )
{}

};

template < typename ExprA, typename ExprB >
VectorTag< VectorPlusVecto r< ExprA, ExprB > >
operator+ ( VectorTag< ExprA > const & a,
VectorTag< ExprB > const & b ) {
return ( VectorPlusVecto r< ExprA, ExprB >( a, b ) );
}

struct Vector : public VectorTag< VectorStoragePo licy > {

Vector ( Number a = 0 )
: VectorTag< VectorStoragePo licy >( a )
{}

template < typename Expr >
Vector & operator= ( VectorTag< Expr > const & other )
{
for ( std::size_t i = 0; i < length; ++i ) {
(*this)[i] = other[i];
}
return ( *this );
}

template < typename Expr >
Vector & operator+= ( VectorTag< Expr > const & other ) {
*this = *this + other;
return ( *this );
}

};
template < typename Expr >
std::ostream & operator<< ( std::ostream & o_str,
VectorTag< Expr > const & v ) {
for ( std::size_t i = 0; i < length; ++i ) {
o_str << v[i] << ' ';
}
return ( o_str );
}
int main ( void ) {
Vector a ( 1.0 );
Vector b ( 2.3 );
a += b;
std::cout << a+b << '\n';
}

As you can see, there is the VectorPlusVecto r template that postpones
evaluation of sums. So if you write

(a + b + c + d)[2];

there is no additional temporary vector but the expression gets translated
into:

a[2] + b[2] + c[2] + d[2]

The challenge is to define a VectorIncrement expression template
(representing +=) and then define + in terms of that. I just don't see how
to do that without loosing the advantage of expression templates
(elimination of temporaries).

Best

Kai-Uwe Bux
Mar 29 '06 #6
"Axter" <go****@axter.c om> writes:
The following is the recommended approach to overloading arithmetic and
assignment operators:

T& T::operator+=(c onst T&){
//.... impl
return *this
}

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


Or, more succinctly,

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

IIRC recommended by Meyers in Effective C++, 3rd Ed.

----------------------------------------------------------------------
Dave Steffen, Ph.D. Fools ignore complexity. Pragmatists
Software Engineer IV suffer it. Some can avoid it. Geniuses
Numerica Corporation remove it.
ph (970) 419-8343 x27 -- Alan Perlis
fax (970) 223-6797
dg*******@numer ica.us
Mar 29 '06 #7
Kai-Uwe Bux wrote:
Axter wrote:
The following is the recommended approach to overloading arithmetic and
assignment operators:

T& T::operator+=(c onst T&){
//.... impl
return *this
}

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


Recommended for which reason?


See C++ Coding Standards by Herb Sutter and Andrei Alexandrescu
Item 27 [Prefer the canonical forms of arithmetic and assignment
operators]

----------------------------------------------------------------------------------------
David Maisonave
http://axter.com

Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------

Apr 6 '06 #8
Axter wrote:
Kai-Uwe Bux wrote:
Axter wrote:
> The following is the recommended approach to overloading arithmetic and
> assignment operators:
>
> T& T::operator+=(c onst T&){
> //.... impl
> return *this
> }
>
> T operator+(const T& lhs, const T& rhs){
> T temp(lhs);
> return temp += rhs;
> }


Recommended for which reason?


See C++ Coding Standards by Herb Sutter and Andrei Alexandrescu
Item 27 [Prefer the canonical forms of arithmetic and assignment
operators]


Thanks for the reference. I just read it. Maybe I am missing something, but
that item does *not* at all contain a discussion of

+ in terms of += vs += in terms of +

Rather, the main concern is consistency: "In general, for some binary
operator @ (be it +, -, *, and so on), you should define its assignment
version such that a @= b and a = a @ b have the same meaning (other than
that the first form might be more efficient and only evaluates a once)."
Then the authors set out to propose the "canonical way" @ in terms of @=
observing that this achieves the stated design goal; but they do not
provide a rational as to why this is superior to the obvious alternative @=
in terms of @.

The next paragraph enters a discussion of the virtues of non-member
implementations for operators. This is undisputed. The section closes with
variations, examples, and finally a notable exception to the canonical
recommendation. This addendum is actually the first point in the item where
the alternative "@= in terms of @" is mentioned.

The way I read it, item 27 (as opposed to Alf's post) provides no technical
reason at all to prefer:

@ in terms of @=

to:

@= in terms of @
Best

Kai-Uwe Bux
Apr 7 '06 #9

Kai-Uwe Bux wrote:
Axter wrote:
Kai-Uwe Bux wrote:
Axter wrote:

> The following is the recommended approach to overloading arithmetic and
> assignment operators:
>
> T& T::operator+=(c onst T&){
> //.... impl
> return *this
> }
>
> T operator+(const T& lhs, const T& rhs){
> T temp(lhs);
> return temp += rhs;
> }

Recommended for which reason?


See C++ Coding Standards by Herb Sutter and Andrei Alexandrescu
Item 27 [Prefer the canonical forms of arithmetic and assignment
operators]


Thanks for the reference. I just read it. Maybe I am missing something, but
that item does *not* at all contain a discussion of

+ in terms of += vs += in terms of +

Rather, the main concern is consistency: "In general, for some binary
operator @ (be it +, -, *, and so on), you should define its assignment
version such that a @= b and a = a @ b have the same meaning (other than
that the first form might be more efficient and only evaluates a once)."
Then the authors set out to propose the "canonical way" @ in terms of @=
observing that this achieves the stated design goal; but they do not
provide a rational as to why this is superior to the obvious alternative @=
in terms of @.

The next paragraph enters a discussion of the virtues of non-member
implementations for operators. This is undisputed. The section closes with
variations, examples, and finally a notable exception to the canonical
recommendation. This addendum is actually the first point in the item where
the alternative "@= in terms of @" is mentioned.

The way I read it, item 27 (as opposed to Alf's post) provides no technical
reason at all to prefer:

@ in terms of @=

to:

@= in terms of @
Best

Kai-Uwe Bux


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.
Your followup post suggested the opposite approach using swap method.
But not all classes have swap method, and infact, most don't. Those
that do, don't always have an efficient swap method.

I'm sure there are times when using the opposite approach would be more
efficient, but in general, I would recommend Herb Sutters method.

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

Apr 8 '06 #10

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

Similar topics

5
6887
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
37400
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
2226
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
1988
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
2643
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
9646
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
9484
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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
8983
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
7505
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
6742
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.