• C++ 中 const 成员函数的本质


    const 成员函数形如:

    class Test {
    public:
      void fun() const;
    };
    
    • 1
    • 2
    • 3
    • 4

    先看一段非常好的英文解释:

    A “const function”, denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.

    重点是下面一段,抽空翻译:
    Another way of thinking about such “const function” is by viewing a class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of member variables are possible.

    It is possible to loosen the “const function” restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a “const function”, these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a “const function” writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)

    As usual when dealing with the const keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const only applies when adding const to the end of the function declaration after the parenthesis.

    const is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const correctness and the const keyword:

    Const correctness

    The C++ ‘const’ Declaration: Why & How

    https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration

    提炼重点:

    const 成员函数 void fun() const 被编译为 void Test_fun(const Test* this)

    const 成员函数 void fun() 被编译为 void Test_fun(Test* this)

    区别在于传入的 this 指针类型不同

    由此引发出了一种有意思的重载,即 const 成员函数重载,如下

    #include
    using namespace std;
     
    class Test
    {
    protected:
        int x;
    public:
        Test (int i):x(i) { }
        void fun() const
        {
            cout << "fun() const called " << endl;
        }
        void fun()
        {
            cout << "fun() called " << endl;
        }
    };
     
    int main()
    {
        Test t1 (10);
        const Test t2 (20);
        t1.fun();
        t2.fun();
        return 0;
    }
    
    • 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

    代码输出为:

    fun() called 
    fun() const called 
    
    • 1
    • 2

    可以这么理解,

    编译后两个 fun 函数分别为 void Test_fun(Test* this)void Test_fun(const Test* this)

    调用 t.fun() 时,实质上是 t.fun(&t)

    t1 类型为 Test&t1 对应 void Test_fun(Test* this)

    t2 类型为 const Test&t2 对应 void Test_fun(const Test* this)

    这就是发生的重载

    由此再引申一点:

    如果注释掉类里面的 const 函数,则报错,无法运行

    如果注释掉 非const 函数,不报错,两次都调用 const 函数

    也就是说,void Test_fun(const Test* this) 可以接收 const非const 类型

    但是 void Test_fun(Test* this) 却只能接收 非const 类型,不能接收 const 类型

  • 相关阅读:
    字符串函数(三)
    git(实现代码存档和同步)
    SpringCloud——服务注册——Consul
    读书笔记:c++对话系列,模板方法模式(Template Method Pattern)
    StringBuffer解析
    贪吃蛇游戏
    Remote Sensing投稿经验分享
    NIO工作方式浅析
    新能源三电系统测试如何快速连接,避免多次插拔?安规测试电连接器的作用
    Linux shell 重定向输入和输出
  • 原文地址:https://blog.csdn.net/qq_37454669/article/details/127824446