473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

operator ++ overload - How To

Hi all,

For my own educational process I am implementing a Fraction class
described in the VSJ magazine to help me understand Visual C# operator
overloading. I'm a relative newbie.

At the moment my basic code is:
using System;
using System.Collections.Generic;
using System.Text;

namespace XYZ.Common
{

//
// This class is to demonstrate how to overload operators such as
+, -, ++.
//

public class Fraction
{
private Int32 numerator;
private Int32 denominator;

// Constructors

// 'Whole numbers'

public Fraction(Int32 numerator)
{
this.numerator = numerator;
this.denominator = 1;
}

// 'True fractions'

public Fraction(Int32 numerator, Int32 denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}

// Implementation of the + operator

public static Fraction operator+(Fraction lhs, Fraction rhs)
{
if (lhs.denominator == rhs.denominator)
{
return new Fraction(lhs.numerator + rhs.numerator,
lhs.denominator);
}
else
{
Int32 denominator = lhs.denominator * rhs.denominator;
Int32 firstProduct = (denominator / lhs.denominator) *
lhs.numerator;
Int32 secondProduct = (denominator / rhs.denominator) *
rhs.numerator;

return new Fraction(firstProduct + secondProduct,
denominator);
}
}

// Implementation of the ++ operator - VERSION 1

public static Fraction operator ++(Fraction f)
{
f.numerator++;
return f;
}

// Implementation of the ++ operator - VERSION 2

public static Fraction operator ++(Fraction f)
{
return new Fraction(f.numerator + 1, f.denominator);
}

public override string ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}

}
}
My problem is that both versions of the operator ++ method compile and
return the correct result.

Version 1 simply returns the original object after amending it. Version
2 creates and returns a totally new object.
Which is the correct version to use with regard to memory allocation
etc?

Thanks for any help.

Oct 22 '06 #1
6 1276
Jo********@hotmail.co.uk wrote:
// Implementation of the ++ operator - VERSION 1
public static Fraction operator ++(Fraction f)
{
f.numerator++;
return f;
}
// Implementation of the ++ operator - VERSION 2
public static Fraction operator ++(Fraction f)
{
return new Fraction(f.numerator + 1, f.denominator);
}
As you've discovered, they both work correctly.

I'd strongly favour version 1, because (i) it doesn't have the
overhead (however small) of creating a new object and performing that
constructor call, and (ii) in the event that you add more fields to
the class, version 1 won't reset the other fields like version 2
would.

Memory management isn't an issue when you're only dealing with simple
Int32 variables.

Eq.
Oct 22 '06 #2
Paul E Collins <fi******************@CL4.orgwrote:
Jo********@hotmail.co.uk wrote:
// Implementation of the ++ operator - VERSION 1
public static Fraction operator ++(Fraction f)
{
f.numerator++;
return f;
}
// Implementation of the ++ operator - VERSION 2
public static Fraction operator ++(Fraction f)
{
return new Fraction(f.numerator + 1, f.denominator);
}

As you've discovered, they both work correctly.

I'd strongly favour version 1, because (i) it doesn't have the
overhead (however small) of creating a new object and performing that
constructor call, and (ii) in the event that you add more fields to
the class, version 1 won't reset the other fields like version 2
would.
But the fact that it doesn't create a new object is the problem with it
as well. It means that after:

Fraction f = ....;
Fraction g = f;

then

f++;

isn't the same as:

f = f+1;

Now, quite possibly the class should actually be a struct, but if it
*does* need to be a class, I'd favour making it an immutable one, like
string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #3
On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>But the fact that it doesn't create a new object is the problem with it
as well. It means that after:

Fraction f = ....;
Fraction g = f;

then

f++;

isn't the same as:

f = f+1;
Correct me if I'm wrong, but surely to add 1 to a fraction, you should
add the denominator into the numerator, not merely increment the
numerator? If your ++ operator did that, then f++ and f = f + 1 would
be the same.

--
Posted via a free Usenet account from http://www.teranews.com

Oct 22 '06 #4
Ben Newsam <be********@ukonline.co.ukwrote:
On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
But the fact that it doesn't create a new object is the problem with it
as well. It means that after:

Fraction f = ....;
Fraction g = f;

then

f++;

isn't the same as:

f = f+1;

Correct me if I'm wrong, but surely to add 1 to a fraction, you should
add the denominator into the numerator, not merely increment the
numerator? If your ++ operator did that, then f++ and f = f + 1 would
be the same.
Leaving the actual addition to one side, the difference is that the
addition operator given returns a *new* object, leaving the old one
with the previous value - whereas the ++ operator given *changes* the
existing value.

But you're quite right - the ++ implementation should increment the
numerator by the denominator, not by one. At least, I'd be surprised by
the results of the current implementation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #5
Quite right about adding the denominator. Thanks. I didn't think it
through!

I think I'll go with version 1.

John.
Jon wrote:
Ben Newsam <be********@ukonline.co.ukwrote:
On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>But the fact that it doesn't create a new object is the problem with it
>as well. It means that after:
>
>Fraction f = ....;
>Fraction g = f;
>
>then
>
>f++;
>
>isn't the same as:
>
>f = f+1;
Correct me if I'm wrong, but surely to add 1 to a fraction, you should
add the denominator into the numerator, not merely increment the
numerator? If your ++ operator did that, then f++ and f = f + 1 would
be the same.

Leaving the actual addition to one side, the difference is that the
addition operator given returns a *new* object, leaving the old one
with the previous value - whereas the ++ operator given *changes* the
existing value.

But you're quite right - the ++ implementation should increment the
numerator by the denominator, not by one. At least, I'd be surprised by
the results of the current implementation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #6
Quite right about adding the denominator. Thanks. I didn't think it
through!

I think I'll go with version 1.

John.
Jon wrote:
Ben Newsam <be********@ukonline.co.ukwrote:
On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>But the fact that it doesn't create a new object is the problem with it
>as well. It means that after:
>
>Fraction f = ....;
>Fraction g = f;
>
>then
>
>f++;
>
>isn't the same as:
>
>f = f+1;
Correct me if I'm wrong, but surely to add 1 to a fraction, you should
add the denominator into the numerator, not merely increment the
numerator? If your ++ operator did that, then f++ and f = f + 1 would
be the same.

Leaving the actual addition to one side, the difference is that the
addition operator given returns a *new* object, leaving the old one
with the previous value - whereas the ++ operator given *changes* the
existing value.

But you're quite right - the ++ implementation should increment the
numerator by the denominator, not by one. At least, I'd be surprised by
the results of the current implementation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #7

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

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
7
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++...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
7
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
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...
3
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,...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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 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.