Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of overloading built-in operator to add support for custom type in C++
#include <iostream>
using namespace std;
class One_value {
int a;
void set_value (int arg) {
a = arg;
// Overload + operator
One_value operator+(const One_value& other_one_value) {
One_value new_value;
new_value.a = this->a + other_one_value.a;
return new_value;
int main () {
One_value firts_instance;
One_value second_instance;
One_value third_instance;
// add two One_value instances
third_instance = firts_instance + second_instance;
cout << third_instance.a << endl;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment