== and != operator (operator overloading) 
Author Message
 == and != operator (operator overloading)

I tried to do something like this:

class Point3D {
        --
        --

        public override bool Equals(object o)
        {
            //code..
        }

        public bool operator ==(Point3D p1)
        {
            return this.Equals(p1);
        }

Quote:
}

The above code doesn't work. Why?

I have to code the == operator as:

public static bool opearator ==(Point3D p1, Point3D p2)
{
        return p1.Equals(p2);

Quote:
}

Now after compilation, the compiler complains about != operator.

I have to add != ().

The wholething is tedious. Any comments?



Wed, 23 Jul 2003 06:24:45 GMT  
 == and != operator (operator overloading)
Equals is the way to do it from the frameworks perspective. You may also do
operator overloads, but if you do ==, you also need to do !=.

The compiler doesn't synthesize them because for nullable types, (a == b) !=
!(a != b)


Quote:
> I tried to do something like this:

> class Point3D {
>         --
>         --

>         public override bool Equals(object o)
>         {
>             //code..
>         }

>         public bool operator ==(Point3D p1)
>         {
>             return this.Equals(p1);
>         }
> }

> The above code doesn't work. Why?

> I have to code the == operator as:

> public static bool opearator ==(Point3D p1, Point3D p2)
> {
>         return p1.Equals(p2);
> }

> Now after compilation, the compiler complains about != operator.

> I have to add != ().

> The wholething is tedious. Any comments?



Wed, 23 Jul 2003 09:08:12 GMT  
 == and != operator (operator overloading)
can you please explain this comment? I do not see why nullable objects
prevent the compiler to define x != y as !(x==y).
Thanks,
- Dominik



Quote:
> Equals is the way to do it from the frameworks perspective. You may also
do
> operator overloads, but if you do ==, you also need to do !=.

> The compiler doesn't synthesize them because for nullable types, (a == b)
!=
> !(a != b)



Thu, 24 Jul 2003 20:55:06 GMT  
 == and != operator (operator overloading)
Eric -

Can you give a short concrete example discussing "nullable type" that
demonstrates
    (a == b) != !(a != b)
?

- Dan

 thate non-associativity of inve


Quote:
> Equals is the way to do it from the frameworks perspective. You may also
do
> operator overloads, but if you do ==, you also need to do !=.

> The compiler doesn't synthesize them because for nullable types, (a == b)
!=
> !(a != b)



> > I tried to do something like this:

> > class Point3D {
> >         --
> >         --

> >         public override bool Equals(object o)
> >         {
> >             file://code..
> >         }

> >         public bool operator ==(Point3D p1)
> >         {
> >             return this.Equals(p1);
> >         }
> > }

> > The above code doesn't work. Why?

> > I have to code the == operator as:

> > public static bool opearator ==(Point3D p1, Point3D p2)
> > {
> >         return p1.Equals(p2);
> > }

> > Now after compilation, the compiler complains about != operator.

> > I have to add != ().

> > The wholething is tedious. Any comments?



Fri, 25 Jul 2003 08:15:58 GMT  
 == and != operator (operator overloading)
Sorry I didn't make this clearer.

Nullable types are ones where "null" is a valid value, in addition to the
usual values. This is *not* the same thing as reference null.

The canonical example is SqlInt32 in System.Data.SqlTypes, with null being a
database null. In SQL expression syntax:

null == null        is false

and

null != null        is also false.


Quote:
> Eric -

> Can you give a short concrete example discussing "nullable type" that
> demonstrates
>     (a == b) != !(a != b)
> ?

> - Dan

>  thate non-associativity of inve


> > Equals is the way to do it from the frameworks perspective. You may also
> do
> > operator overloads, but if you do ==, you also need to do !=.

> > The compiler doesn't synthesize them because for nullable types, (a ==
b)
> !=
> > !(a != b)



> > > I tried to do something like this:

> > > class Point3D {
> > >         --
> > >         --

> > >         public override bool Equals(object o)
> > >         {
> > >             file://code..
> > >         }

> > >         public bool operator ==(Point3D p1)
> > >         {
> > >             return this.Equals(p1);
> > >         }
> > > }

> > > The above code doesn't work. Why?

> > > I have to code the == operator as:

> > > public static bool opearator ==(Point3D p1, Point3D p2)
> > > {
> > >         return p1.Equals(p2);
> > > }

> > > Now after compilation, the compiler complains about != operator.

> > > I have to add != ().

> > > The wholething is tedious. Any comments?



Sat, 26 Jul 2003 00:28:35 GMT  
 == and != operator (operator overloading)
I think that (a == b) != !(a != b) is a very rare case.

So wouldn't it be a better choice?:
    if we define either operator == or operator !=, not both, then
        the other operator is generated automatically.
    if we define both operators, just use them.

I don't think it would be difficult for the compiler writers.

What do you think?

Jinu



Quote:
> Sorry I didn't make this clearer.

> Nullable types are ones where "null" is a valid value, in addition to the
> usual values. This is *not* the same thing as reference null.

> The canonical example is SqlInt32 in System.Data.SqlTypes, with null being
a
> database null. In SQL expression syntax:

> null == null        is false

> and

> null != null        is also false.



> > Eric -

> > Can you give a short concrete example discussing "nullable type" that
> > demonstrates
> >     (a == b) != !(a != b)
> > ?

> > - Dan

> >  thate non-associativity of inve


> > > Equals is the way to do it from the frameworks perspective. You may
also
> > do
> > > operator overloads, but if you do ==, you also need to do !=.

> > > The compiler doesn't synthesize them because for nullable types, (a ==
> b)
> > !=
> > > !(a != b)



> > > > I tried to do something like this:

> > > > class Point3D {
> > > >         --
> > > >         --

> > > >         public override bool Equals(object o)
> > > >         {
> > > >             file://code..
> > > >         }

> > > >         public bool operator ==(Point3D p1)
> > > >         {
> > > >             return this.Equals(p1);
> > > >         }
> > > > }

> > > > The above code doesn't work. Why?

> > > > I have to code the == operator as:

> > > > public static bool opearator ==(Point3D p1, Point3D p2)
> > > > {
> > > >         return p1.Equals(p2);
> > > > }

> > > > Now after compilation, the compiler complains about != operator.

> > > > I have to add != ().

> > > > The wholething is tedious. Any comments?



Tue, 05 Aug 2003 13:33:02 GMT  
 == and != operator (operator overloading)
I disagree, the null case comes up quite often in db programming and it's
just smart to have included consideration for it in the language.
Furthermore, requiring implementation of both == and != at the same
eliminates a lot of bugs and ambiguous class behavior that's possible
without such requirements.

--
Joe Mayo
C# Station - Information, Links, and Other
Resources for the C# Programming Language
http://www.csharp-station.com


Quote:
> I think that (a == b) != !(a != b) is a very rare case.

> So wouldn't it be a better choice?:
>     if we define either operator == or operator !=, not both, then
>         the other operator is generated automatically.
>     if we define both operators, just use them.

> I don't think it would be difficult for the compiler writers.

> What do you think?

> Jinu



> > Sorry I didn't make this clearer.

> > Nullable types are ones where "null" is a valid value, in addition to
the
> > usual values. This is *not* the same thing as reference null.

> > The canonical example is SqlInt32 in System.Data.SqlTypes, with null
being
> a
> > database null. In SQL expression syntax:

> > null == null        is false

> > and

> > null != null        is also false.



> > > Eric -

> > > Can you give a short concrete example discussing "nullable type" that
> > > demonstrates
> > >     (a == b) != !(a != b)
> > > ?

> > > - Dan

> > >  thate non-associativity of inve

message

> > > > Equals is the way to do it from the frameworks perspective. You may
> also
> > > do
> > > > operator overloads, but if you do ==, you also need to do !=.

> > > > The compiler doesn't synthesize them because for nullable types, (a
==
> > b)
> > > !=
> > > > !(a != b)



> > > > > I tried to do something like this:

> > > > > class Point3D {
> > > > >         --
> > > > >         --

> > > > >         public override bool Equals(object o)
> > > > >         {
> > > > >             file://code..
> > > > >         }

> > > > >         public bool operator ==(Point3D p1)
> > > > >         {
> > > > >             return this.Equals(p1);
> > > > >         }
> > > > > }

> > > > > The above code doesn't work. Why?

> > > > > I have to code the == operator as:

> > > > > public static bool opearator ==(Point3D p1, Point3D p2)
> > > > > {
> > > > >         return p1.Equals(p2);
> > > > > }

> > > > > Now after compilation, the compiler complains about != operator.

> > > > > I have to add != ().

> > > > > The wholething is tedious. Any comments?



Wed, 06 Aug 2003 11:50:38 GMT  
 == and != operator (operator overloading)
Actually, the null case comes up quite often in db programming and it's
just smart to have included consideration for it in the language.
Furthermore, requiring implementation of both == and != at the same
eliminates a lot of bugs and ambiguous class behavior that's possible
without such requirements.

--
Joe Mayo
C# Station - Information, Links, and Other
Resources for the C# Programming Language
http://www.csharp-station.com


Quote:
> I think that (a == b) != !(a != b) is a very rare case.

> So wouldn't it be a better choice?:
>     if we define either operator == or operator !=, not both, then
>         the other operator is generated automatically.
>     if we define both operators, just use them.

> I don't think it would be difficult for the compiler writers.

> What do you think?

> Jinu



> > Sorry I didn't make this clearer.

> > Nullable types are ones where "null" is a valid value, in addition to
the
> > usual values. This is *not* the same thing as reference null.

> > The canonical example is SqlInt32 in System.Data.SqlTypes, with null
being
> a
> > database null. In SQL expression syntax:

> > null == null        is false

> > and

> > null != null        is also false.



> > > Eric -

> > > Can you give a short concrete example discussing "nullable type" that
> > > demonstrates
> > >     (a == b) != !(a != b)
> > > ?

> > > - Dan

> > >  thate non-associativity of inve

message

> > > > Equals is the way to do it from the frameworks perspective. You may
> also
> > > do
> > > > operator overloads, but if you do ==, you also need to do !=.

> > > > The compiler doesn't synthesize them because for nullable types, (a
==
> > b)
> > > !=
> > > > !(a != b)



> > > > > I tried to do something like this:

> > > > > class Point3D {
> > > > >         --
> > > > >         --

> > > > >         public override bool Equals(object o)
> > > > >         {
> > > > >             file://code..
> > > > >         }

> > > > >         public bool operator ==(Point3D p1)
> > > > >         {
> > > > >             return this.Equals(p1);
> > > > >         }
> > > > > }

> > > > > The above code doesn't work. Why?

> > > > > I have to code the == operator as:

> > > > > public static bool opearator ==(Point3D p1, Point3D p2)
> > > > > {
> > > > >         return p1.Equals(p2);
> > > > > }

> > > > > Now after compilation, the compiler complains about != operator.

> > > > > I have to add != ().

> > > > > The wholething is tedious. Any comments?



Wed, 06 Aug 2003 11:50:58 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

2. Using C# operator overloading in VB.NET

3. Can't overload operator new in VC7.

4. HELP: Overloading Assignment Operators in MC++

5. Overloading Assignment Operators in MC++

6. stupid operator overloading syntax

7. Wrapping unmanaged operator overloads?

8. overloading the new and delete operator

9. overloading operator ==

10. class templates and overloaded operators

11. Overloading Operator Problem; Need Help

12. Operator overloading in C

 

 
Powered by phpBB® Forum Software