Operator Overloading in C++

By , last updated April 6, 2018

Operator overloading in C++ is a vast topic. There are 38 operators available for overloading, and each operator has its own set of rules and best practices.

There are unary and binary operators, and one ternary operator. Most operators can be overloaded.

Lets start with some simple c++ operator overloading example. You can try all the examples out by creating a new project in VS and copy pasting the code.

Addition of two objects

In this example we will show overloading addition assignment operator, operator plus (+). With a class containing a std::string, you can add those two classes with operator + and operator +=:

The result would be a third instance, which is the sum of the two first instances. If you do Hello + World! the sum should be Hello World!.

class HelloOperator
{
    public:
        HelloOperator(std::string word)
            : m_Word(word)
        {}

        HelloOperator & operator+=(const HelloOperator & rhs)
        {
            m_Word += rhs.m_Word;
            return *this;
        }

        std::string operator()() const
        {
            return m_Word;
        }

    private:
        std::string m_Word;
};

inline HelloOperator operator+(HelloOperator lhs, const HelloOperator &rhs)
{
    lhs += rhs;

    return lhs;
}

Just use + between hello and world to get things going.

HelloOperator hello("Hello ");
HelloOperator world("World!\n");
HelloOperator hello_world = hello + world;

std::cout << hello_world();

Output will be Hello World!.

Overriding arithmetic operators plus and minus

In this example we will show overloading arithmetic addition and subtraction operators, operators plus (+) and minus (-). With a class containing some integer Number, you can add and subtract the values with operator + and operator += and operator - and operator -=:

The result would be a third instance, which is the sum or the difference of the two first instances.

class HelloOperator
{
public:
	HelloOperator(int number)
		: m_Number(number)
	{}

	HelloOperator & operator+=(const HelloOperator & rhs)
	{
		m_Number += rhs.m_Number;
		return *this;
	}

	HelloOperator & operator-=(const HelloOperator & rhs)
	{
		m_Number -= rhs.m_Number;
		return *this;
	}

	int operator()() const
	{
		return m_Number;
	}

private:
	int m_Number;
};

// operator +
inline HelloOperator operator+(HelloOperator lhs, const HelloOperator &rhs)
{
	lhs += rhs;

	return lhs;
}

// operator -
inline HelloOperator operator-(HelloOperator lhs, const HelloOperator &rhs)
{
	lhs -= rhs;

	return lhs;
}

Create new instances of this class and subtract them from each other:

HelloOperator hello(10);
HelloOperator world(20);
HelloOperator hello_world = hello - world;

std::cout << hello_world();

The output will be -10.

Function call operator overloading

The function operator allows you to call your class as if it were a method.

With the function operator operator(), you can call your class just like regular method.

class HelloFunctionCall
{
    public:
        std::string operator()()
        {
            return "Hello World!\n";
        }
};

The usage is:

HelloFunctionCall call;
std::cout << call();

Make an instance of the class call, and call the function operator with call(). That method returns Hello World!, which is printed in the console window.

One of the nice things with C++, is the possibility to overload operators with custom behaviors. One of those uses are with math libraries, where adding and multiplying matrices is not trivial. For multiplying matrices, there are a couple of rules regarding matrix multiplication. The sizes of the two matrices must be compatible (not necessarily equal size) and there is a special way of multiplying. All of this can overloaded operators do behind the scenes.

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.