Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Operator overloading
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== In this case, the addition operator is overloaded to allow addition on a user-defined type {{code|Time}} in [[C++]]: <syntaxhighlight lang=Cpp> 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; } </syntaxhighlight> Addition is a [[binary operation]], which means it has two [[operand]]s. In C++, the arguments being passed are the operands, and the {{code|temp}} object is the returned value. The operation could also be defined as a class method, replacing {{code|lhs}} by the hidden {{code|this}} argument; However, this forces the left operand to be of type {{code|Time}}: <syntaxhighlight lang=Cpp> // 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; } </syntaxhighlight> Note that a [[Unary operation|unary]] operator defined as a class method would receive no apparent argument (it only works from {{code|this}}): <syntaxhighlight lang=Cpp> bool Time::operator!() const { return hours == 0 && minutes == 0 && seconds == 0; } </syntaxhighlight> The less-than (<) operator is often overloaded to sort a structure or class: <syntaxhighlight lang=Cpp> class Pair { public: bool operator<(const Pair& p) const { if (x_ == p.x_) { return y_ < p.y_; } return x_ < p.x_; } private: int x_; int y_; }; </syntaxhighlight> Like with the previous examples, in the last example operator overloading is done within the class. In C++, after overloading the less-than operator (<), [[sort (C++)|standard sorting functions]] can be used to sort some classes.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Operator overloading
(section)
Add topic