RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1083735
Accepted
zcorvid
zcorvid
Asked:2020-02-17 18:50:41 +0000 UTC2020-02-17 18:50:41 +0000 UTC 2020-02-17 18:50:41 +0000 UTC

为什么 const 方法可以调用非 const 类成员方法?

  • 772

在下面的代码中, const 类方法B调用了其成员的非 const 方法A &a_ref,这似乎与const限定符的逻辑相矛盾(理论上,代码不应该编译)。为什么会发生这种情况?

最低代码:

#include <iostream>

class A
{
public:
    void NonConstHello() /*const*/
    {
        std::cout << "non const Hello from A!" << std::endl;
    }
};

class B
{
public:
    A &a_ref;

    explicit B(A &a) : a_ref(a) {}
    void ConstHello() const /*const method*/
    {
        a_ref.NonConstHello();  // calls non-const method for his member???
        std::cout << "    const Hello from B!" << std::endl;
    }
};

int main()
{
    A a;
    B b(a);
    b.ConstHello();
    return 0;
}

结论:

non const Hello from A!
    const Hello from B!
c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    zcorvid
    2020-02-17T18:50:41Z2020-02-17T18:50:41Z

    在上面的代码中,没有调用 const 对象上的非常量方法

    问题中的代码等效于以下内容:

    #include <iostream>
    
    class A
    {
    public:
        void NonConstHello() /*const*/
        {
            std::cout << "non const Hello from A!" << std::endl;
        }
    };
    
    class B
    {
    public:
    // pointer instead of reference
        A *a_ptr;
    
        explicit B(A *a) : a_ptr(a) {}
        void ConstHello() const /*const method*/
        {
            (*a_ptr).NonConstHello();
            std::cout << "    const Hello from B!" << std::endl;
        }
    };
    
    int main()
    {
        A a;
        B b(&a);
        b.ConstHello();
        return 0;
    }
    

    此外,它在功能上和链接的定义上都是等价的。

    让我们弄清楚const-qualifier 发生了什么。

    类方法的常量性B意味着它的所有成员都变成常量,就像值一样,也就是说,指针变成常量,得到一个类型A * const(而不是const A*!)。

    这意味着我们不能从该方法将指针更改为地址,但可以合法地更改数据,对非常量方法的调用不是在类成员上发生,而是在其取消引用的结果上发生。

    解引用运算符是 const (它不能更改地址)并返回对我们合法应用非常量方法的对象的非常量引用。


    让我们回到带有链接的原始示例。

    现在一切都清楚了。如果非常粗略,引用是“在必要时隐式转换为对象的指针”。调用链接上的方法实际上并不是调用链接上的方法,而是调用位于相应地址的对象上的方法。

    方法的常量性B禁止我们更改作为链接的成员(即更改其地址),但不禁止更改链接指向我们的数据,我们通过调用非常量方法。

    粗略地说,a_ref在常量类方法中,B它有一个类型 not const A&,但是A & const(公平地说,我们注意到引用无论如何都不能改变,它只初始化一次,它的地址不再改变,实际上它是,因为地址是不变的)。

    • 1

相关问题

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5