|
-
December 17th, 2008, 09:10 PM
#1
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.......
-
December 17th, 2008, 09:51 PM
#2
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.......
-
December 18th, 2008, 05:09 AM
#3
Re: Operator overloading
 Originally Posted by rliq
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.
-
December 18th, 2008, 06:10 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|