[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/language/operator_assignment [Back]  [Original]

cppreference.com
cppreference.com

cppreference.com

.

​​ ( 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);

  • *this, *this, , . ( void).
  • T2 , T

a b (b ). -, .

a b, , (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, .

cv- , , .

, , .

, ( , )

  • E1 ,
  • E1 = {} E1 = T{}, T E1.
  • E1 = {E2} E1 = T{E2}, T E1.
  • E1 , E1 = {args...} , , . : , , / E1 = {} , {} , {} .
( C++11)

lvalue volatile- , .

( C++20)

, T, :

<tbody> </tbody>
T*& operator=(T*&, T*);
T*volatile & operator=(T*volatile &, T*);

T, volatile-, :

<tbody> </tbody>
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, :

<tbody> </tbody>
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 = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b

++a --a a++ a--

+a -a a + b a - b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b

!a a && b a || b

a == b a != b a < b a > b a <= b a >= b a <=> b

a[...] *a &a a->b a.b a->*b a.*b

a(...)
a, b
a ? b : c

static_cast
dynamic_cast
const_cast cv
reinterpret_cast
C static_cast, const_cast reinterpret_cast
new
delete , new,
sizeof
sizeof... ( C++11)
typeid
noexcept , ( C++11)
alignof ( C++11)


Web Proxy Viewer  |  New URL  |  Original Page