Question about const_cast in c++

后端 未结 4 583
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 02:41

all: this is quoted from Effective C++ 3rd editiion

const_cast is typically used to cast away the constness of objects. It is the only C++-style ca

4条回答
  •  春和景丽
    2020-12-16 03:14

    const_cast can only be used to cast to pointers and references. It can't be used to cast to objects. Here's why: if you have a const object you can't make it non-const and vice versa - it's already const, you can't redeclare it. You can only try to access it through a pointer or reference without (or with) const.

提交回复
热议问题