第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

請(qǐng)問(wèn)這里的const_cast是什么用法?

請(qǐng)問(wèn)這里的const_cast是什么用法?

C++ C
慕斯709654 2022-04-22 15:15:37
const_cast不是去除底層const的作用嗎,這里怎么反倒顯示轉(zhuǎn)化為const 引用了
查看完整描述

1 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

const_cast conversion

Converts between types with different cv-qualification.

Syntax

const_cast < new_type > ( expression )    

Returns a value of type new_type.

Explanation

Only the following conversions can be done with const_cast. In particular, only const_cast may be used to cast away (remove) constness or volatility.

1) Two possibly multilevel pointers to the same type may be converted between each other, regardless of cv-qualifiers at each level.2) lvalue of any type T may be converted to a lvalue or rvalue reference to the same type T, more or less cv-qualified. Likewise, an rvalue may be converted to a more or less cv-qualified rvalue reference. The result of a reference const_cast refers to the original object if expression is a glvalue and to the materialized temporaryotherwise (since C++17).3) Same rules apply to possibly multilevel pointers to data members and possibly multilevel pointers to arrays of known and unknown bound (arrays to cv-qualified elements are considered to be cv-qualified themselves) (since C++17)4) null pointer value may be converted to the null pointer value of new_type

As with all cast expressions, the result is:

  • an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;

  • an xvalue if new_type is an rvalue reference to object type;

  • a prvalue otherwise.

  • Notes

    Pointers to functions and pointers to member functions are not subject to const_cast

    const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

    Keywords

    const_cast

    Example

  • Run this code

1#include <iostream> struct type {    type() :i(3) {}    void m1(int v) const {        // this->i = v;                 // compile error: this is a pointer to const        const_cast<type*>(this)->i = v; // OK as long as the type object isn't const    }    int i;}; int main() {    int i = 3;                    // i is not declared const    const int& cref_i = i;     const_cast<int&>(cref_i) = 4; // OK: modifies i    std::cout << "i = " << i << '\n'    type t; // note, if this is const type t;, then t.m1(4); is UB    t.m1(4);    std::cout << "type::i = " << t.i << '\n';     const int j = 3; // j is declared const    int* pj = const_cast<int*>(&j);    // *pj = 4;         // undefined behavior!     void (type::*mfp)(int) const = &type::m1; // pointer to member function//  const_cast<void(type::*)(int)>(mfp); // compiler error: const_cast does not                                         // work on function pointers  }

    Output:

  • i = 4 type::i = 4


 


查看完整回答
反對(duì) 回復(fù) 2022-04-24
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)