.
| | ( class T)
| |||
|---|---|---|---|---|
a = b
|
T& T::operator =(const T2& b);
|
/ | ||
a += b
|
T& T::operator +=(const T2& b);
|
T& operator +=(T& a, const T2& b);
| ||
a -= b
|
T& T::operator -=(const T2& b);
|
T& operator -=(T& a, const T2& b);
| ||
a *= b
|
T& T::operator *=(const T2& b);
|
T& operator *=(T& a, const T2& b);
| ||
a /= b
|
T& T::operator /=(const T2& b);
|
T& operator /=(T& a, const T2& b);
| ||
a %= b
|
T& T::operator %=(const T2& b);
|
T& operator %=(T& a, const T2& b);
| ||
a &= b
|
T& T::operator &=(const T2& b);
|
T& operator &=(T& a, const T2& b);
| ||
a |= b
|
T& T::operator |=(const T2& b);
|
T& operator |=(T& a, const T2& b);
| ||
a ^= b
|
T& T::operator ^=(const T2& b);
|
T& operator ^=(T& a, const T2& b);
| ||
a <<= b
|
T& T::operator <<=(const T2& b);
|
T& operator <<=(T& a, const T2& b);
| ||
a >>= b
|
T& T::operator >>=(const T2& b);
|
T& operator >>=(T& a, const T2& b);
| ||
|
| ||||
| ( C++11) |
.
a a b.
lhs = rhs
|
(1) | ||||||||
lhs = {}
|
(2) | ( C++11) | |||||||
lhs = { rhs }
|
(3) | ( C++11) | |||||||
, lhs , rhs lhs.
lvalue rvalue ( C++11) lvalue, .
, , .
, ( , )
|
|
( C++11) |
| ( C++20) |
T*& operator=(T*&, T*); |
||
T*volatile & operator=(T*volatile &, T*); |
||
T, volatile-, :
T& operator=(T&, T ); |
||
A1 A2, A1 ( volatile-), A2 ,
<tbody> </tbody> A1& operator=(A1&, A2); |
||
#include <iostream>
int main()
{
int n = 0; //
n = 1; //
std::cout << n << ' ';
n = {}; // ,
std::cout << n << ' ';
n = 'a'; // ,
std::cout << n << ' ';
n = {'b'}; // ,
std::cout << n << ' ';
n = 1.0; // ,
std::cout << n << ' ';
// n = {1.0}; // ( )
int& r = n; //
r = 2; //
std::cout << n << ' ';
int* p;
p = &n; //
p = nullptr; // ,
std::cout << p << ' ';
struct {int a; std::string s;} obj;
obj = {1, "abc"}; //
std::cout << obj.a << ':' << obj.s << '\n';
}
:
1 0 97 98 1 2 0 1:abc
| lhs op rhs | (1) | ||||||||
| lhs op {} | (2) | ( C++11) | |||||||
| lhs op {rhs} | (3) | ( C++11) | |||||||
| op | *=, /= %=, += -=, <<=, >>=, &=, ^=, |=
| |
| lhs | lhs , , op += -=, , + -
| |
| rhs | rhs lhs |
E1 op= E2 ( E1 lvalue, E2 rvalue ( C++11)) , E1 = E1 op E2, , E1 (, f(a += b, g()), += , g()).
, A1 A2, A1 ( volatile-), A2 , :
<tbody> </tbody> A1& operator*=(A1&, A2); |
||
A1& operator/=(A1&, A2); |
||
A1& operator+=(A1&, A2); |
||
A1& operator-=(A1&, A2); |
||
I1 I2, I1 ( volatile-), I2 , :
<tbody> </tbody> I1& operator%=(I1&, I2); |
||
I1& operator<<=(I1&, I2); |
||
I1& operator>>=(I1&, I2); |
||
I1& operator&=(I1&, I2); |
||
I1& operator^=(I1&, I2); |
||
I1& operator|=(I1&, I2); |
||
cv- T, :
T*& operator+=(T*&, std::ptrdiff_t); |
||
T*& operator-=(T*&, std::ptrdiff_t); |
||
T*volatile & operator+=(T*volatile &, std::ptrdiff_t); |
||
T*volatile & operator-=(T*volatile &, std::ptrdiff_t); |
||
| : |
C++:
| CWG 1527 | C++11 | , |
|
| CWG 1538 | C++11 | E1 = {E2} E1 = T(E2) (T E1), C
|
E1 = T{E2}
|
| WG | C++20 | volatile , |
|
| CWG 2654 | C++20 | volatile |
|
|
|
|
|
|
|
|
a(...)
| ||||||
a, b
| ||||||
a ? b : c
| ||||||
|
static_cast | ||||||