CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Operator overloading

    Say I wanted to override the '+' operator for the following class:

    Code:
    public class MyClass
    {
        private MyType _mySecretInternalRepresentation;  // MyType maybe UInt32 for example
    
        public static MyClass operator+(MyClass a, MyClass b)
        {
            // code required here
        }
    }
    Ok, *I*know how to add a to b, but how can i get at their internal representation so I can perform the add, without exposing the internal representation (making it public) to everyone and breaking one of the fundamental laws of OO ?

    Am I missing something? I'm sure in C++ I had access to private members of the *same* class within an operator overload function.

    Microsoft give an example in the documentation (see operator overloading C# vs Java) for complex numbers, but their ComplexNumber class has public members which is not good because individual parts of the complex number can be modified independently by external code.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Operator overloading

    Sorted....

    If the operator+ function is NOT static, then you do get access to the parameters private members:
    Code:
    public class MyClass
    {
        private MyType _mySecretInternalRepresentation;  // MyType maybe UInt32 for example
    
        public MyClass operator+(MyClass other)
        {
            other._mySecretInternalRepresentation;  // can access here
        }
    }
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Operator overloading

    Quote Originally Posted by rliq View Post
    Sorted....

    If the operator+ function is NOT static, then you do get access to the parameters private members:
    Code:
    public class MyClass
    {
        private MyType _mySecretInternalRepresentation;  // MyType maybe UInt32 for example
    
        public MyClass operator+(MyClass other)
        {
            other._mySecretInternalRepresentation;  // can access here
        }
    }
    Did you try compiling that? It doesn't compile, the operator overload must be static.


    Ok, *I*know how to add a to b, but how can i get at their internal representation so I can perform the add, without exposing the internal representation (making it public) to everyone and breaking one of the fundamental laws of OO ?
    You should have just tried rather than asking It works exactly the way you'd expect it to:

    Code:
            public class MyClass
            {
                private int value;  // MyType maybe UInt32 for example
    
                public static MyClass operator +(MyClass left, MyClass right)
                {
                    return new MyClass { value = left.value + right.value };
                }
            }
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Operator overloading

    As you've found out the operator+ method is a member of the class (even though it's static) so it has access to the private data of instances of the same class.

    C# doesn't support instance-level operating overloading (it's not C++). The correct way of overloading an operator is to use a static method as you have done.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured