[Feature Request] operator overloading: #201
Labels
enhancement
New feature or request
gssoc-ext
Contributions made as part of GirlScript Summer of Code Extended Edition.
hacktoberfest-accepted
PRs accepted for Hacktoberfest 2024. Ensures contributions are counted towards the official Hackt...
level2
GirlScript Summer of Code | Contributor's Levels
🚀 Feature Request
content for operator overloading:
Added fundamental theory
Examples for better understanding
Rules and pointers for the concept
Reviewed the content added
4. Operator Overloading
Introduction
Operator overloading allows you to redefine the way operators work for user-defined types (classes and structs). It enables you to specify more intuitive ways to perform operations on objects of your classes.
Overloading an operator does not change:
built-in types.
Syntax
An overloaded operator is implemented as a special member function with the keyword
operator
followed by the symbol of the operator being overloaded.Example
Types of Operators that Can Be Overloaded
+
,-
,*
,/
,%
==
,!=
,<
,>
,<=
,>=
&&
,||
,!
&
,|
,^
,~
,<<
,>>
++
,--
=
,+=
,-=
,*=
,/=
,%=
[]
()
->
,.
(only for pointers to members)>>
,<<
Operators that cannot be overloaded include:
::
,.*
,.
,? :
,sizeof
Example:
Operator Overloading Rules
When overloading operators, there are several rules to keep in mind:
operator+
should return a new object, whileoperator+=
should return a reference to*this
.a == b
should return the same result asb == a
.Would you like to work on this feature?
The text was updated successfully, but these errors were encountered: