1. <legend id='BUgsj'><style id='BUgsj'><dir id='BUgsj'><q id='BUgsj'></q></dir></style></legend>
    <i id='BUgsj'><tr id='BUgsj'><dt id='BUgsj'><q id='BUgsj'><span id='BUgsj'><b id='BUgsj'><form id='BUgsj'><ins id='BUgsj'></ins><ul id='BUgsj'></ul><sub id='BUgsj'></sub></form><legend id='BUgsj'></legend><bdo id='BUgsj'><pre id='BUgsj'><center id='BUgsj'></center></pre></bdo></b><th id='BUgsj'></th></span></q></dt></tr></i><div id='BUgsj'><tfoot id='BUgsj'></tfoot><dl id='BUgsj'><fieldset id='BUgsj'></fieldset></dl></div>
      <bdo id='BUgsj'></bdo><ul id='BUgsj'></ul>

    <small id='BUgsj'></small><noframes id='BUgsj'>

    1. <tfoot id='BUgsj'></tfoot>
    2. c++中关于const_cast的问题

      Question about const_cast in c++(c++中关于const_cast的问题)
    3. <legend id='kxXwC'><style id='kxXwC'><dir id='kxXwC'><q id='kxXwC'></q></dir></style></legend>
      1. <small id='kxXwC'></small><noframes id='kxXwC'>

      2. <tfoot id='kxXwC'></tfoot>

          <i id='kxXwC'><tr id='kxXwC'><dt id='kxXwC'><q id='kxXwC'><span id='kxXwC'><b id='kxXwC'><form id='kxXwC'><ins id='kxXwC'></ins><ul id='kxXwC'></ul><sub id='kxXwC'></sub></form><legend id='kxXwC'></legend><bdo id='kxXwC'><pre id='kxXwC'><center id='kxXwC'></center></pre></bdo></b><th id='kxXwC'></th></span></q></dt></tr></i><div id='kxXwC'><tfoot id='kxXwC'></tfoot><dl id='kxXwC'><fieldset id='kxXwC'></fieldset></dl></div>
            <tbody id='kxXwC'></tbody>

              <bdo id='kxXwC'></bdo><ul id='kxXwC'></ul>

                本文介绍了c++中关于const_cast的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                全部:这是引自 Effective C++ 3rd edition

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

                const_cast 通常用于抛弃对象的常量性.这是唯一可以做到这一点的 C++ 风格的强制转换.

                const_cast is typically used to cast away the constness of objects. It is the only C++-style cast that can do this.

                我的问题是 const_cast 可以为非常量对象添加常量性吗?其实我写了一个小程序来验证我的想法.

                My question is can const_cast add constness to a non-const object? Actually i wrote a small programme trying to approve my thought.

                class ConstTest
                {
                 public:
                
                 void test() {
                    printf("calling non-const version test const function 
                ");
                }
                
                 void test() const{
                    printf("calling const version test const function 
                ");
                
                } 
                
                };
                 int main(int argc,char* argv){
                 ConstTest ct;
                 const ConstTest cct;
                 cct.test();
                 const_cast<const ConstTest&>(ct).test();//it is wrong to write this statement without the '&',why
                
                }
                

                省略&"导致以下错误:

                Omitting the '&' incurs error below:

                错误 C2440:const_cast":无法从ConstTest"转换为const ConstTest"

                error C2440: 'const_cast' : cannot convert from 'ConstTest' to 'const ConstTest'

                说明const_cast可以增加constness,但是好像必须强制转换为一个对象引用,这个引用有什么魔力?

                It shows that const_cast can add constness,but seems like you have to cast to a object reference, what is the magic of this reference ?

                推荐答案

                你不需要const_cast来添加constness:

                You don't need const_cast to add constness:

                class C;
                C c;
                C const& const_c = c;
                

                反过来需要一个const_cast

                const C const_c;
                C& c = const_cast<C&>(const_c);
                

                但是如果您尝试在 c 上使用非常量操作,则行为未定义.

                but behavior is undefined if you try to use non-const operations on c.

                顺便说一句,如果不使用引用,则要制作对象的副本:

                By the way, if you don't use a reference, a copy of the object is to be made:

                C d = const_c; // Copies const_c
                

                这篇关于c++中关于const_cast的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                What is a dangling reference?(什么是悬空引用?)
                C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                <small id='67L6T'></small><noframes id='67L6T'>

                1. <tfoot id='67L6T'></tfoot>
                    <tbody id='67L6T'></tbody>
                    <bdo id='67L6T'></bdo><ul id='67L6T'></ul>
                    <i id='67L6T'><tr id='67L6T'><dt id='67L6T'><q id='67L6T'><span id='67L6T'><b id='67L6T'><form id='67L6T'><ins id='67L6T'></ins><ul id='67L6T'></ul><sub id='67L6T'></sub></form><legend id='67L6T'></legend><bdo id='67L6T'><pre id='67L6T'><center id='67L6T'></center></pre></bdo></b><th id='67L6T'></th></span></q></dt></tr></i><div id='67L6T'><tfoot id='67L6T'></tfoot><dl id='67L6T'><fieldset id='67L6T'></fieldset></dl></div>

                      • <legend id='67L6T'><style id='67L6T'><dir id='67L6T'><q id='67L6T'></q></dir></style></legend>