Home > Guides > Programming > C/C++

Operator Overloading

Last updated Mar 1, 2004.

Object-oriented (OO) programming languages treat user-defined types as first-class citizens. One of the manifestations of this principle is that programmers can extend the semantics of built-in operators to support user-defined types as if they were built-in types. Such an extension overloads rather than overrides the predefined meaning of an operator.

NOTE

C++ requires at least one of the operands of an overloaded operator to be a user-defined type.

Although you can use ordinary functions for the same purpose, operator overloading provides a uniform notational convention that's clearer than the ordinary function call syntax. Consider the following example:

Monday < Tuesday; //overloaded <
Greater_than(Monday, Tuesday);

Clearly, the use of an overloaded < operator is a better choice.

The capacity to redefine the meaning of a built-in operator in C++ was a source of criticism. People (mostly C programmers making the migration to C++) felt that overloading an operator was as dangerous as enabling the programmer to add, remove, or change keywords of the language. Still, notwithstanding the potential Tower of Babel that might arise as a result, operator overloading is one of the most fundamental features of C++ and is mandatory in generic programming, as the following sections will show. Today, even languages that tried to do without operator overloading are in the process of adding this feature.

An overloaded operator is merely a function whose name is an operator preceded by the keyword operator. The following code listing defines an overloaded version of operator < that compares two Book objects and returns the one with the lower ISBN (for example, to sort a book collection by ISBN):

class Book
{
private:
 long ISBN;
public:
//...
 long get_ISBN() const { return ISBN;}
};

//overloaded version of <
bool operator < (const Book& b1, const Book& b2)
{
 return b1.get_ISBN() < b2.get_ISBN();
}

Discussions

Bugzilla
Posted Nov 18, 2008 01:53 AM by cupu
2 Replies
auto_ptr issues
Posted Sep 14, 2007 07:43 AM by singh_siddhu
1 Replies
i want c++ book through net
Posted Aug 23, 2007 11:13 PM by harivilu
3 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevBjarne Stroustrups's Stevens Talk
By Danny KalevDecember 7, 2009 No Comments

On 2nd December Bjarne Stroustrup delivered a talk about the standardization process of C++0x at the Stevens institute. Here some of the key points from his talk.

Danny KalevMinutes from the October 2009 Meeting
By Danny KalevNovember 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny KalevOctober 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

See More Blogs

Informit Network