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

const_cast cppreference.com
cppreference.com

const_cast

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
(lvalue, rvalue, xvalue)
( )
-(C++11)
,
(C++11)
(C++11)
:
:  
:
:
:
:
:
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||b, a&&b, !a
a==b, a!=b, a<b, a>b, a<=b, a>=b, a<=>b ( C++20)
a[b], *a, &a, a->b, a.b, a->*b, a.*b
a(...), a,b, a?b:c
new
delete
throw
alignof
sizeof
sizeof...(C++11)
typeid
noexcept(C++11)
(C++17)
(C++20)
const_cast
static_cast
reinterpret_cast
dynamic_cast
: (T)a, T(a), auto(a), auto{a} ( C++23)
 

cv-.

const_cast< - >( )

-.

const_cast . , const_cast () .

1) , , , cv- .
2) lvalue T lvalue rvalue T, cv-. , prvalue x rvalue cv-. const_cast glvalue ( C++17).
3) , , ( cv- cv-) ( C++17)
4) -

, :

  • lvalue, - lvalue rvalue ( C++11);
  • xvalue, - rvalue ;
( C++11)
  • prvalue.

- const_cast

const_cast , volatile , volatile . volatile volatile glvalue .

const_cast

#include <iostream>

struct type
{
    int i;

    type(): i(3) {}

    void f(int v) const
    {
        // this->i = v;                 //  :    const
        const_cast<type*>(this)->i = v; // ,      
    }
};

int main() 
{
    int i = 3;                 // i   
    const int& rci = i; 
    const_cast<int&>(rci) = 4; // OK:  i
    std::cout << "i = " << i << '\n';

    type t; //       t,   t.f(4)  
            // 
    t.f(4);
    std::cout << "type::i = " << t.i << '\n';

    const int j = 3; // j  
    [[maybe_unused]]
    int* pj = const_cast<int*>(&j);
    // *pj = 4;      //  

    [[maybe_unused]]
    void (type::* pmf)(int) const = &type::f; //   -
    // const_cast<void(type::*)(int)>(pmf);   //  : const_cast
                                              //      
}

:

i = 4
type::i = 4


Web Proxy Viewer  |  New URL  |  Original Page