为什么const_cast删除指针的constness但不删除指向const的指针? [英] Why does const_cast remove constness for a pointer but not for a pointer to a const?

查看:49
本文介绍了为什么const_cast删除指针的constness但不删除指向const的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 const_cast 可以使用指针和引用.

I understand that const_cast works with pointers and references.

我假设 const_cast 的输入应该是指针或引用.我想知道如果输入是对 const int 的指针/引用,为什么不删除constness?

I'm assuming that the input to const_cast should be a pointer or reference. I want to know why it doesn't remove the constness if the input is a pointer/reference to a const int?

以下代码按预期工作.

  1. const_cast (带有多级指针)

  1. const_cast with multilevel pointers

int main()
{
    using std::cout;
    #define endl '\n'
    const int * ip = new int(123);
    const int * ptr = ip;
    *const_cast<int*>(ptr) = 321;
    cout << "*ip: " << *ip << endl;  // value of *ip is changed to 321
}

但是当我尝试指向 const int 或指向 const int 的指针时,该值似乎没有变化.

But when I try a pointer to const int or reference to const int, the value doesn't seem to change.

const_cast 引用const int

const_cast with reference to const int

int main()
{
    using std::cout;
    #define endl '\n'
    const int i = 123;
    const int & ri = i;
    const_cast<int&>(ri) = 321;
    cout << "i: " << i << endl;  // value in 'i' is 123
}

  • const_cast (带有指向const int的指针)

  • const_cast with pointer to const int

    int main()
    {
        using std::cout;
        #define endl '\n'
        const int i = 123;
        const int * ri = &i;
        *const_cast<int*>(ri) = 321;
        cout << "i: " << i << endl;  // value in 'i' is 123
    }
    

  • (1)可以正常工作,但我无法理解为什么(2)&(3)不能按我认为的方式工作,尽管 const_cast 的输入是指针/引用.

    (1) works as expected, but I'm unable to comprehend why (2) & (3) don't work the way I think though the input to the const_cast is a pointer/reference.

    请帮助我了解其背后的哲学.谢谢.

    Please help me understand the philosophy behind this. Thanks.

    推荐答案

    有两种常数.

    恒定性是对象的固有属性.无法更改.

    Constness of an object is an inherent property of an object. It cannot be changed.

    想想一本印刷书籍中的一页.可以将其视为字符串,并且无法更改.它说出了它的意思,仅此而已.因此,这是一个 const字符串.

    Think of a page in a printed book. It can be viewed as a string of characters, and it cannot be changed. It says what it says and that's it. So it's a const string.

    现在想想一块黑板.它可能上面写着一些东西.您可以擦除它并写其他东西.因此黑板是一个非常量 string .

    Now think of a blackboard. It may have something written on it. You can wipe that and write something else. So the blackboard is a non-const string.

    另一种常量性是指针和引用常量.此常数不是指向对象的固有属性,而是 permission .它表示不允许您通过该指针修改对象 .它没有说明对象本身是否可以修改.

    The other kind of constness is pointer and reference constness. This constness is not an inherent property of the pointed-to object, but a permission. It says you are not allowed to modify the object through this pointer. It says nothing about whether the object itself can be modified.

    因此,如果您有const指针,则不一定知道它真正指向的是什么.也许是书页.也许是黑板.指针不告诉您.

    So if you have a const pointer, you don't necessarily know what it really points to. Maybe it's a book page. Maybe it's a blackboard. The pointer doesn't tell.

    现在,如果您确实知道它确实是一块黑板,那么您可能会很讨厌并要求获得许可才能继续操作并更改上面写的内容.这就是const_cast所做的.它允许您做某事.

    Now if you do know somehow that it is indeed a blackboard, you can be nasty and demand permission to go ahead and change what's written on it. That's what const_cast does. It gives you permission to do something.

    如果您需要修改字符串的权限,而结果却是打印页面,该怎么办?您得到了您的许可,然后继续擦除它……然后……确切发生的是 undefined .也许什么都没有.也许打印件被弄脏了,您既无法识别原始字符串,也无法在其上写下任何内容.也许您的世界爆炸成碎片.您可以尝试看看,但不能保证明天也会发生同样的事情.

    What happens if you demand permission to modify a string, and it turns out to be a printed page? You get your permission, you go ahead and wipe it... and... What exactly happens is undefined. Perhaps nothing at all. Perhaps the print is smeared and you can neither recognise the original string nor write anything on top. Perhaps your world explodes to little pieces. You can try and see, but there's no guarantee the same thing will happen tomorrow.

    这篇关于为什么const_cast删除指针的constness但不删除指向const的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆