• const对象竟然可以修改其成员,只因顶层const


    评论区的大哥的解释很有道理,在下面贴出来,我收回之前的言论:
    class::func( ) const成员函数的返回值是string& 不是const string&,这是问题所在。
    class::func( ) const内的一切类成员都是constthis指针是const*,ps类型是const string*return*ps类型是const string返回为string&,有些编译器会警告报错。请严格控制返回值类型是否const,非const_cast<>不解const

    ==================== 以上2022.11.5更新

    我自定义了一个类 HasPtr,并创建了一个它的常量对象,get_str 返回成员的引用。

    #include 
    #include "HasPtr_ex30test.h"
    
    using std::cout;
    
    int main()
    {
        const HasPtr hp(0, "hello");
        cout << hp.get_str() << '\n';
        hp.get_str() = "jesus";
        cout << hp.get_str() << '\n';
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    输出:

    hello
    jesus

    看到这一幕真的很震惊,const 对象的成员竟然被修改了。这究竟怎么回事,下面给出头文件。

    #ifndef HASPTR_EX30TEST_H
    #define HASPTR_EX30TEST_H
    
    #include 
    
    using std::string;
    
    class HasPtr
    {
        friend void swap(HasPtr &, HasPtr &);
    
    public:
        HasPtr(int i_, const string &s = "") : ps(new string(s)), i(i_) {}
        HasPtr(const HasPtr &hp) : ps(new string(*hp.ps)), i(hp.i) {}
        HasPtr &operator=(const HasPtr &hp);
        ~HasPtr() { delete ps; }
        string &get_str() const
        {
            return *ps;
        }
    
    private:
        string *ps;
        int i;
    };
    // 其余函数
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    关键就在 get_str() 成员函数上,如果 ps 是个 string 对象,肯定是不能修改的了,但偏偏它是一个 string 指针。这会发生什么呢?

    return *ps; 相当于 return *((*this).ps) 。我们知道这是一个常量成员函数,所以 *this 是常量,所以 (*this).ps 也是常量,也是一个 string 指针。问题来了,这个常量是顶层还是底层?答案是顶层。如果不信,我们做如下改动:

        string &get_str() const
        {
            *ps = "ggg";
            return *ps;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    main 函数不变,输出:

    ggg

    ggg

    可以看到在常量成员函数中成功修改了 *ps 的值,是不是不可思议。其实不能修改的是 ps ,但 *ps 确实可以修改,这也印证了 ps 就是顶层 const

    *((*this).ps) 是对顶层 const 解引用,当然也就丢弃了常量特性了。所以非常量可以绑定到 string & 上面,进而可以在 main 函数中修改为 "jesus"

  • 相关阅读:
    Polygon Miden:扩展以太坊功能集的ZK-optimized rollup
    CTFshow web(文件上传158-161)
    【MySQL】左连接右连接内连接与Hash连接、子查询原理与实战(MySQL专栏启动)
    大模型重塑软件研发,从辅助编程到多 Agent 协同还有多远?| 新程序员
    Mall脚手架总结(四) —— SpringBoot整合RabbitMQ实现超时订单处理
    前端对用户名密码加密处理,后端解密
    排序算法的空间复杂度和时间复杂度
    Spring工具类--CollectionUtils的使用
    Spring开发:动态代理的艺术与实践
    centos离线安装telnet、traceroute工具
  • 原文地址:https://blog.csdn.net/jasminefeng/article/details/127693442