C++ 中 const的使用

1、const可以定义常量,编码中尽量使用const代替define定义的常量

2、使用const保护参数

void fun(const int a)
{
    a=10;//compile error
}

3、const引用作为参数,可以提高效率,不用每个参数都会进行拷贝操作。

4、类成员函数后面加上const,该函数不会修改类中的数据成员

猜你喜欢

转载自blog.csdn.net/jiaojinlin/article/details/85954690