Operator Overloading

lxXTaCoXxl 0 Tallied Votes 156 Views Share

I get tired of having to google this simple line of code all the time. My problem with memorizing it is that I switch back and forth between C# and C++ all the time, and the way you overload operators in each is different. So once I learn one again, I forget the other... >_< No fun lol.

Anyways I figure this will be useful for anyone else out there that needs to know how to overload operators with custom user types. For example using a vector style user type:

public static TriVector operator +(TriVector o1, TriVector o2) {
    return new TriVector(o1.X + o2.X, o1.Y + o2.Y, o1.Z + o2.Z);

Hope it helps some others as well.

public static UserTypeNameHere operator +(UserTypeNameHere Operand1, UserTypeNameHere Operand2) {
	return new UserTypeNameHere(Operand1.VariableName + Operand2.VariableName);