Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with override the operators

Problem with override the operators

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Those are binary operators. MSDN[^]

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    V Offline
    V Offline
    VendorX
    wrote on last edited by
    #3

    ...and..? On MSDN they are overloadable...

    L 1 Reply Last reply
    0
    • V VendorX

      I'm trying to override those operators:

      ==, !=, <, >, <=, >=, <<, >>

      ...but VS10 give me this error:

      Overloadable unary operator expected

      Is there some special conditions to make this work..? Cause those operators are overloadable...

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #4

      And we'd need to see your code where you're overloading these operators to tell you what you're doing wrong.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • V VendorX

        ...and..? On MSDN they are overloadable...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        They're also overridable in your code. Did you check the example? There's a link at the bottom of the page.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        1 Reply Last reply
        0
        • V VendorX

          I'm trying to override those operators:

          ==, !=, <, >, <=, >=, <<, >>

          ...but VS10 give me this error:

          Overloadable unary operator expected

          Is there some special conditions to make this work..? Cause those operators are overloadable...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          See http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx[^].

          Use the best guess

          1 Reply Last reply
          0
          • V VendorX

            I'm trying to override those operators:

            ==, !=, <, >, <=, >=, <<, >>

            ...but VS10 give me this error:

            Overloadable unary operator expected

            Is there some special conditions to make this work..? Cause those operators are overloadable...

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #7

            As Eddy states, those aren't Unary operators. This would indicate that these aren't the ones that are causing you problems. Could you post a sample that shows the code that the compiler is complaining about?

            I was brought up to respect my elders. I don't respect many people nowadays.
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            V 1 Reply Last reply
            0
            • P Pete OHanlon

              As Eddy states, those aren't Unary operators. This would indicate that these aren't the ones that are causing you problems. Could you post a sample that shows the code that the compiler is complaining about?

              I was brought up to respect my elders. I don't respect many people nowadays.
              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              V Offline
              V Offline
              VendorX
              wrote on last edited by
              #8

              OK, thanks guys for the links, but I've already seen this. Problem is that I'm trying to convert simple class from C++ to CS. Here is the code:

              class SOME_API SomeClass
              {
              public:
              // Accessors.
              bool operator==( const SomeClass& Other ) const
              {
              return Index == Other.Index;
              }

              bool operator!=( const SomeClass& Other ) const
              {
                  return Index != Other.Index;
              }
              
              //...
              

              private:
              // SomeClass index.
              int Index;

              //...
              

              };

              Is there any chance to convert this to CS without writing a bunch of code?

              D 1 Reply Last reply
              0
              • V VendorX

                OK, thanks guys for the links, but I've already seen this. Problem is that I'm trying to convert simple class from C++ to CS. Here is the code:

                class SOME_API SomeClass
                {
                public:
                // Accessors.
                bool operator==( const SomeClass& Other ) const
                {
                return Index == Other.Index;
                }

                bool operator!=( const SomeClass& Other ) const
                {
                    return Index != Other.Index;
                }
                
                //...
                

                private:
                // SomeClass index.
                int Index;

                //...
                

                };

                Is there any chance to convert this to CS without writing a bunch of code?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #9

                A rough untested example of how I do this for a class (it's much simpler for a struct as it can never be null):

                // An immutable int wrapper class
                public class SomeClass : IEquatable<SomeClass>
                {
                private int index;

                public SomeClass(int index)
                {
                    this.index = index;
                }
                
                public static bool operator ==(SomeClass instance, SomeClass other)
                {
                    if(object.ReferenceEquals(instance, other))
                        return true;
                    if(object.ReferenceEquals(null, instance) || object.ReferenceEquals(null, other))
                        return false;
                    return instance.index == other.index;
                }
                public static bool operator !=(SomeClass instance, SomeClass other)
                {
                    return !(instance == other);
                }
                
                public int Index
                {
                    get { return index; }
                }
                
                public override bool Equals(object obj)
                {
                    return Equals(obj as SomeClass);
                }
                public bool Equals(SomeClass other)
                {
                    if(object.ReferenceEquals(null, other))
                        return false;
                    return index.Equals(other.index);
                }
                public override int GetHashCode()
                {
                    return index;
                }
                

                }

                And a struct:

                // An immutable int wrapper struct
                public struct SomeStruct : IEquatable<SomeStruct>
                {
                private int index;

                public SomeStruct(int index)
                {
                    this.index = index;
                }
                
                public static bool operator ==(SomeStruct instance, SomeStruct other)
                {
                    return instance.index == other.index;
                }
                public static bool operator !=(SomeStruct instance, SomeStruct other)
                {
                    return !(instance == other);
                }
                
                public int Index
                {
                    get { return index; }
                }
                
                public override bool Equals(object obj)
                {
                    if(obj is SomeStruct)
                        return Equals((SomeStruct)obj);
                    return false;
                }
                public bool Equals(SomeStruct other)
                {
                    return index.Equals(other.index);
                }
                public override int GetHashCode()
                {
                    return index;
                }
                

                }

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pra

                1 Reply Last reply
                0
                • V VendorX

                  I'm trying to override those operators:

                  ==, !=, <, >, <=, >=, <<, >>

                  ...but VS10 give me this error:

                  Overloadable unary operator expected

                  Is there some special conditions to make this work..? Cause those operators are overloadable...

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #10

                  Unary operators require one parameter, so it's assuming that the operator should be a unary one as you are only supplying one parameter. The operators you have listed are binary operators, so require two parameters. See my example in my other post and my article An Introduction to Operator Overloading in C#[^]

                  Dave
                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  V 1 Reply Last reply
                  0
                  • D DaveyM69

                    Unary operators require one parameter, so it's assuming that the operator should be a unary one as you are only supplying one parameter. The operators you have listed are binary operators, so require two parameters. See my example in my other post and my article An Introduction to Operator Overloading in C#[^]

                    Dave
                    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    V Offline
                    V Offline
                    VendorX
                    wrote on last edited by
                    #11

                    Thanks for the snippets DaveyM69 - I've ended up with something similar. For now, to compare I'm using GetIndex(), but my converted code is constantly growing. Didn't know that CS is so much more complicated... Thanks again...

                    M 1 Reply Last reply
                    0
                    • V VendorX

                      Thanks for the snippets DaveyM69 - I've ended up with something similar. For now, to compare I'm using GetIndex(), but my converted code is constantly growing. Didn't know that CS is so much more complicated... Thanks again...

                      M Offline
                      M Offline
                      MicroVirus
                      wrote on last edited by
                      #12

                      I don't believe C# is much more complicated with this; I think the real difference is that you'd typically let the overloaded operators be member functions in C++ and C# wants them to be static functions. So if you rewrite your C++ code to the static overloading, then you're already having the same code in C# and C++.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups