Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operators in C and C++
(section)
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!
===Assignment=== C and C++ have the same assignment operators and all can be overloaded in C++. For the combination operators, <code>a β= b</code> (where <code>β</code> represents an operation) is equivalent to <code>a = a β b</code>, except that <code>a</code> is evaluated only once. {| class="wikitable" style="width:100%" ! rowspan="2" | Operation ! rowspan="2" | Syntax ! colspan="2" | C++ prototype |- ! in class K ! outside class |- ! {{rh}} | [[Assignment operator (C++)|Assignment]] | style="text-align:center;" | {{nowrap| 1=<code>a '''=''' b</code>}} | {{cpp|1=R& K::operator =(S b);}} | {{n/a}} |- ! {{rh}} | Addition combination | align="center" | <code>a '''+=''' b</code> | {{cpp|1=R& K::operator +=(S b);}} | {{cpp|1=R& operator +=(K& a, S b);}} |- ! {{rh}} | Subtraction combination | style="text-align:center;" | <code>a '''-=''' b</code> | {{cpp|1=R& K::operator -=(S b);}} | {{cpp|1=R& operator -=(K& a, S b);}} |- ! {{rh}} | Multiplication combination | style="text-align:center;" | <code>a '''*=''' b</code> | {{cpp|1=R& K::operator *=(S b);}} | {{cpp|1=R& operator *=(K& a, S b);}} |- ! {{rh}} | Division combination | style="text-align:center;" | <code>a '''/=''' b</code> | {{cpp|1=R& K::operator /=(S b);}} | {{cpp|1=R& operator /=(K& a, S b);}} |- ! {{rh}} | Modulo combination | style="text-align:center;" | <code>a '''%=''' b</code> | {{cpp|1=R& K::operator %=(S b);}} | {{cpp|1=R& operator %=(K& a, S b);}} |- ! {{rh}} | Bitwise AND combination | style="text-align:center;" | <code>a '''&=''' b</code> | {{cpp|1=R& K::operator &=(S b);}} | {{cpp|1=R& operator &=(K& a, S b);}} |- ! {{rh}} | Bitwise OR combination | style="text-align:center;" | <code>a '''<nowiki>|</nowiki>=''' b</code> | {{cpp|1=R& K::operator {{!}}=(S b);|lang=cpp}} | {{cpp|1=R& operator {{!}}=(K& a, S b);|lang=cpp}} |- ! {{rh}} | Bitwise XOR combination | style="text-align:center;" | <code>a '''^=''' b</code> | {{cpp|1=R& K::operator ^=(S b);}} | {{cpp|1=R& operator ^=(K& a, S b);}} |- ! {{rh}} | Bitwise left shift combination | style="text-align:center;" | <code>a '''<<=''' b</code> | {{cpp|1=R& K::operator <<=(S b);}} | {{cpp|1=R& operator <<=(K& a, S b);}} |- ! {{rh}} | Bitwise right shift combination{{Refn | name="rightbitshift" | group="lower-alpha" | According to the C99 standard, the right shift of a negative number is implementation defined. Most implementations, e.g., the GCC,<ref name="Integers">{{Citation | contribution = Integers implementation | url = //gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Integers-implementation.html#Integers-implementation | title = GCC 4.3.3 | publisher = GNU}}.</ref> use an [[arithmetic shift]] (i.e., sign extension), but a [[logical shift]] is possible.}} | style="text-align:center;" | <code>a '''>>=''' b</code> | {{cpp|1=R& K::operator >>=(S b);}} | {{cpp|1=R& operator >>=(K& a, S b);}} |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)