rliq
December 17th, 2008, 08:10 PM
Say I wanted to override the '+' operator for the following class:
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.
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.