Category: 

What is Operator Overloading?

Article Details
  • Written By: Rodney A. Crater
  • Edited By: Allegra J. Lingo
  • Image By: ビッグアップジャパン
  • Last Modified Date: 07 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
Roughly one-fifth of the world's stock of gold - worth over $200 billion USD - is stored under the streets of London.  more...

September 30 ,  1949 :  The Berlin Air Lift ended.  more...

Operator overloading is a computer programming language feature that allows a programmer to override a default operator. When the operator function is overloaded, it may be declared with custom arguments that are different data types than what the operator was capable of handling by default. The new parameters may be primitive or user defined types. Within the body of the overriding function, a programmer may manipulate the parameter values as desired. By using operator overloading, a programmer can create intuitive shortcuts for manipulating user defined data types or modify how default data types are handled.

When user defined data types are created, there is no way to do simple operational tasks, such as adding two of them together or incrementing internal data by using the unary increment operator with the data type. Operators, including the addition symbol, can be overridden so that if they are used as a binary operators between two custom data types they return a user defined result. In the same way, an unary operator, like the incrementation operator, might be overridden to increase an internal value within the user defined type.

Ad

Once an operator is overloaded, the compiler has to make a decision about what to do when it encounters an overloaded operator. The compiler decides the proper action to take based on what data types are being used as operands, or in conjunction with, the operator. If the operands, which are parameter values to the operator function, are user defined data types, the compiler will choose the overloaded function that has parameters which match the operands being used.

Computer languages vary in how they allow this type of ad-hoc polymorphism. Different languages may allow the creation of new custom operators, the overloading of all or some of its operators, or may not allow operator overloading at all. C++, C#, PHP, and Perl all allow some of their operators to be overloaded. Operator overloading is not allowed in C, Java, and Javascript.

Both operator overloading and function overloading in general have been the subject of some debate. Some claim that giving the programmer the freedom of changing the way an operator behaves could introduce the potential for creating unintentional errors by other programmers who use the customized code. The opposing viewpoint proposes that allowing operator overloading gives programmers the capability of creating intuitive and concise code that will be easier to understand, cost less to produce, and reduce errors.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

exception 'Exception' with message 'error writing captcha: Duplicate entry '2147483647' for key 'PRIMARY'' in /ssd/www/wisegeek/public_html/_core/classes/public/Captcha.php:44
Stack trace:
#0 /ssd/www/wisegeek/public_html/_core/controls/public/ControlDiscussionPostBox.php(324): Captcha->createCaptcha()
#1 /ssd/www/wisegeek/public_html/framework/classes/Control.php(104): ControlDiscussionPostBox->preRender(false)
#2 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): Control->render()
#3 /ssd/www/wisegeek/public_html/tpl/default-nocustom-lu/pages/public/article/article.htm(526): Control->__toString()
#4 /ssd/www/wisegeek/public_html/framework/classes/Control.php(300): require('/ssd/www/wisege...')
#5 /ssd/www/wisegeek/public_html/framework/classes/Control.php(309): Control->requireTpl('pages/public/ar...', Object(PageArticleCom), true)
#6 /ssd/www/wisegeek/public_html/framework/classes/Control.php(131): Control->renderTpl('pages/public/ar...', Object(PageArticleCom))
#7 /ssd/www/wisegeek/public_html/framework/classes/FormDataControl.php(87): Control->renderTemplate()
#8 /ssd/www/wisegeek/public_html/framework/classes/Control.php(109): FormDataControl->renderTemplate()
#9 /ssd/www/wisegeek/public_html/framework/classes/ScriptPage.php(50): Control->render(false)
#10 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): ScriptPage->render()
#11 /ssd/www/wisegeek/public_html/framework/classes/Page.php(97): Control->__toString()
#12 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(443): Page->processRequest()
#13 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(7): PublicFrontController->renderPage()
#14 /ssd/www/wisegeek/public_html/index.php(11): PublicFrontController::run()
#15 {main}