[{"@context":"http:\/\/schema.org\/","@type":"BlogPosting","@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/operator-overloading-wikipedia\/#BlogPosting","mainEntityOfPage":"https:\/\/wiki.edu.vn\/en\/wiki14\/operator-overloading-wikipedia\/","headline":"Operator overloading – Wikipedia","name":"Operator overloading – Wikipedia","description":"before-content-x4 Feature of some programming languages after-content-x4 In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a","datePublished":"2022-03-06","dateModified":"2022-03-06","author":{"@type":"Person","@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/author\/lordneo\/#Person","name":"lordneo","url":"https:\/\/wiki.edu.vn\/en\/wiki14\/author\/lordneo\/","image":{"@type":"ImageObject","@id":"https:\/\/secure.gravatar.com\/avatar\/44a4cee54c4c053e967fe3e7d054edd4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/44a4cee54c4c053e967fe3e7d054edd4?s=96&d=mm&r=g","height":96,"width":96}},"publisher":{"@type":"Organization","name":"Enzyklop\u00e4die","logo":{"@type":"ImageObject","@id":"https:\/\/wiki.edu.vn\/wiki4\/wp-content\/uploads\/2023\/08\/download.jpg","url":"https:\/\/wiki.edu.vn\/wiki4\/wp-content\/uploads\/2023\/08\/download.jpg","width":600,"height":60}},"image":{"@type":"ImageObject","@id":"https:\/\/wiki.edu.vn\/wiki4\/wp-content\/uploads\/2023\/08\/download.jpg","url":"https:\/\/wiki.edu.vn\/wiki4\/wp-content\/uploads\/2023\/08\/download.jpg","width":100,"height":100},"url":"https:\/\/wiki.edu.vn\/en\/wiki14\/operator-overloading-wikipedia\/","wordCount":9257,"articleBody":" (adsbygoogle = window.adsbygoogle || []).push({});before-content-x4Feature of some programming languages (adsbygoogle = window.adsbygoogle || []).push({});after-content-x4In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.Table of Contents (adsbygoogle = window.adsbygoogle || []).push({});after-content-x4Rationale [edit]Examples[edit]Criticisms[edit]Catalog[edit]Timeline of operator overloading[edit]1960s[edit]1980s[edit]1990s[edit]2000s[edit]See also[edit]References[edit]Rationale [edit]Operator overloading is syntactic sugar, and is used because it allows programming using notation nearer to the target domain[1] and allows user-defined types a similar level of syntactic support as types built into a language. It is common, for example, in scientific computing, where it allows computing representations of mathematical objects to be manipulated with the same syntax as on paper.Operator overloading does not change the expressive power of a language (with functions), as it can be emulated using function calls. For example, consider variables a, b and c of some user-defined type, such as matrices:a + b * cIn a language that supports operator overloading, and with the usual assumption that the ‘*’ operator has higher precedence than the ‘+’ operator, this is a concise way of writing: (adsbygoogle = window.adsbygoogle || []).push({});after-content-x4Add(a, Multiply(b, c))However, the former syntax reflects common mathematical usage.Examples[edit]In this case, the addition operator is overloaded to allow addition on a user-defined type Time in C++:Time operator+(const Time& lhs, const Time& rhs) { Time temp = lhs; temp.seconds += rhs.seconds; temp.minutes += temp.seconds \/ 60; temp.seconds %= 60; temp.minutes += rhs.minutes; temp.hours += temp.minutes \/ 60; temp.minutes %= 60; temp.hours += rhs.hours; return temp;}Addition is a binary operation, which means it has two operands. In C++, the arguments being passed are the operands, and the temp object is the returned value.The operation could also be defined as a class method, replacing lhs by the hidden this argument; However, this forces the left operand to be of type Time:\/\/ The \"const\" right before the opening curly brace means that |this| is not modified.Time Time::operator+(const Time& rhs) const { Time temp = *this; \/\/ |this| should not be modified, so make a copy. temp.seconds += rhs.seconds; temp.minutes += temp.seconds \/ 60; temp.seconds %= 60; temp.minutes += rhs.minutes; temp.hours += temp.minutes \/ 60; temp.minutes %= 60; temp.hours += rhs.hours; return temp;}Note that a unary operator defined as a class method would receive no apparent argument (it only works from this):bool Time::operator!() const { return hours == 0 && minutes == 0 && seconds == 0;}The less-than ("},{"@context":"http:\/\/schema.org\/","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/#breadcrumbitem","name":"Enzyklop\u00e4die"}},{"@type":"ListItem","position":2,"item":{"@id":"https:\/\/wiki.edu.vn\/en\/wiki14\/operator-overloading-wikipedia\/#breadcrumbitem","name":"Operator overloading – Wikipedia"}}]}]