473,399 Members | 2,774 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,399 software developers and data experts.

overloading of conversion operators

Hi all,

i just played around a little with the c# operator overloading features
and an idea came to my mind. You all possibly know the Nullable<T>
Datatype. Now i thought it would be a nice idea to write my own
Revertable<Ttype that maintains the values of multiple assignments and
can revert to a previous value. An Example to make it clear :

Revertable<intir0 = 123;
ir0 = 500;
ir0 = 700;
ir0.Revert(2)
int x = ir0;

now x should be equal to 123

This stuff could be used in some kind of simplified in-memory
transactions to help maintain state consitency.

I started up and wrote something like this :

public class Revertable<T>
{
private List<TvalueList = new List<T>();

private Revertable(T value)
{
valueList.Add(value);
}

private T Current
{
get
{
return valueList[valueList.Count - 1];
}
}

public void Revert(int times)
{
...
}

public static implicit operator Revertable<T>(T value)
{
return new Revertable<T>(value);
}

public static implicit operator T(Revertable<Tvalue)
{
return value.Current;
}
}

Now the problem is, that the implicit conversion operator creates a
new instance of Revertable<Twhen an assignment happens. So the
previously assigned value is lost :

Revertable<inti = 10;
i = 11; // now i is a new instance of Revertable<intand the previously
assigned value is lost

Is there a way to convert to a given instance, instead of creating a new
one ? I saw that it is impossible to overload the assignment operator
in c# . I know that i could easily add an Assign(T value) method to my
revertable class but that would make the code much less readable.

Perhaps i have to take another path and take a language that supports
custom assignment operator if one exists ?!

Thanks in advance for your ideas and suggestions

Philipp
Sep 5 '07 #1
3 1872
On Sep 5, 9:21 am, Philipp Brune <her...@gmx.dewrote:
i just played around a little with the c# operator overloading features
and an idea came to my mind. You all possibly know the Nullable<T>
Datatype. Now i thought it would be a nice idea to write my own
Revertable<Ttype that maintains the values of multiple assignments and
can revert to a previous value.
<snip>
Now the problem is, that the implicit conversion operator creates a
new instance of Revertable<Twhen an assignment happens. So the
previously assigned value is lost :
Indeed - that's how the assignment operator works, everywhere. The
previous value is lost. Having a type where assignment uses a previous
value feels very counterintuitive to me.

Personally I don't like overloading operators in any but the most
clearcut cases (TimeSpan, DateTime etc) but you might consider
overloading + instead. Then you could do:

Revertable<inti = 10;
i += 11;

Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...

Jon

Sep 5 '07 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
Revertable<inti = 10;
i += 11;

Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...
Maybe the OP could use some other operator which is less common than +.
For example:
Revertable<strings = "Hello";
s <<= "Bye";

The <<= would look like you are "pushing" a new value into the stack of
values in s.
Sep 5 '07 #3
Jon, Alberto

thank you both for the quick reply :-) I had the idea to "abuse" a
binary operator too, something like

r ^= 12345

and additionaly let Revertable<Tprovide a Xor Method if T itself has
an ^ operator defined.

The other way would be to provide a property setter to take

r.V = 12345

but both of them are easy to forget and less comfortable. So i have
to take that my idea (in that special way i wanted it) is impossible to do.

Philipp

Alberto Poblacion schrieb:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>Revertable<inti = 10;
i += 11;

Unfortunately that might *look* like the result should be 21, not a
history of 10, 11...

Maybe the OP could use some other operator which is less common than
+. For example:
Revertable<strings = "Hello";
s <<= "Bye";

The <<= would look like you are "pushing" a new value into the stack
of values in s.

Sep 5 '07 #4

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

Similar topics

2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
2
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this...
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
0
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
2
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 &...
1
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.