第三周、方法后的const重新认识

2017-11-05  本文已影响0人  China帅

1、可以设置函数为const,如:

void f() const {}

表示在这个函数中this为const,即此函数不修改任何成员变量的值,这种使用方式需要在原型和定义时都加上const

2、若类中的一个成员变量定义为const,也必须要进行初始化。在类外进行初始化

3、void fun(){}和void fun()const{} 构成重载关系:

void fun(){}              ====》    void fun(class *this){}

void fun()const{}      ====》    void fun(const class *this){}

上一篇 下一篇

猜你喜欢

热点阅读