Re: How to Create Custom Events? by jp2msft
jp2msft
Thu Aug 28 12:38:06 CDT 2008
Beautiful! I'm writing all this down in an email that I'm forwarding to
myself for safe keeping.
I can't wait to start messing around with this new trick.
Thanks, Mr. Duniho!
"Peter Duniho" wrote:
> On Thu, 28 Aug 2008 08:28:02 -0700, jp2msft
> <jp2msft@discussions.microsoft.com> wrote:
>
> > [...]
> > Could the class be modified so that the workings of it are more
> > transparent
> > (i.e. more like an 'int' or whatever value I am trying to mimic)?
> >
> > Example:
> > MyInt Count = 0;
> > Count += intNewValue;
>
> Sure. You can overload the + operator for the class, to allow instances
> of the class to be added to each other, as well as to provide for adding
> an int to your class.
>
> That said, I'll warn that things may start to get confusing if you do
> this. Operator overloads can work very nicely for value types, but for a
> mutable class, having a binary operator (for example) modify one of the
> operands can result in some very non-intuitive code.
>
> In particular, you can't overload +=. You have to overload +, and then
> the compiler uses that overload when implementing +=. That means that if
> this:
>
> MyInt Count = new MyInt(0); // no overload of assignment operator
> Count += intNewValue;
>
> did what you expected, then this:
>
> MyInt Count = new MyInt(0);
> MyInt Other = Count + intNewValue;
>
> might not do what you expect ("Other" would wind up referencing the same
> instance as "Count", and that single instance of MyInt would have been
> modified).
>
> That's because the overload would have to look like this:
>
> public static MyInt operator +(MyInt op1, int op2)
> {
> op1.N += op2;
> return op1;
> }
>
> Now, all that said, you could write this:
>
> MyInt Count = new MyInt(0); // no overload of assignment operator
> Count.N += intNewValue;
>
> without doing any operator overloading. That's almost as concise as your
> desired syntax, but preserves the normal semantics for a mutable reference
> type.
>
> Pete
>