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.

61 Views
public static UserTypeNameHere operator +(UserTypeNameHere Operand1, UserTypeNameHere Operand2) {
	return new UserTypeNameHere(Operand1.VariableName + Operand2.VariableName);
}
About the Author

I'm a very difficult person to describe because I'm all over the place. If I could sum me up into one word it would be insane. I'm an incredibly vibrant person. I love surfing, playing guitar, programming, photography, video games, skating, snowboarding, running parkour, hanging out with friends, spending time with my girlfriend, and much more. I'm currently enrolled in college courses and pursuing jobs in my area. Ask if you want to know more.