c++ 函数后面有个 const

非静态成员函数后面加const,类似如下函数:

class testClass{
public:
    void testClass() const{ /*...*/}
private:
    /*...*/
};

表示成员函数隐含传入的this指针为 const指针,

决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的,因为隐含了对this指针的const引用。

就是说不会改变对象中成员变量的数值。

 

参考:

https://stackoverflow.com/questions/2157458/using-const-in-classs-functions

猜你喜欢

转载自www.cnblogs.com/hellowooorld/p/11279606.html