const和mutable

xiaoxiao2021-02-28  40

#pragma once #include <iostream> class constmutable { public: int a; int b; int c; const int d=0;//常量是必须存在初始化 mutable int e;//限定了不被const所限制 public: void setabc(int a, int b, int c) { this->a = a; this->b = b; this->c = c; } void showabc() const //(1)定义该函数为只读函数,不可改变类的数据成员; //已经定义成const的成员函数,一旦企图修改数据成员的值,则编译器报错 //(2)若const(加到非成员函数或静态成员后面会产生编译错误) //(3)对mutable修饰的数据成员无效。 //(4)函数声明前加const表示返回值是const,后加const表示不可修改class的成员 { //const作用于成员函数,可以限定不对成员变量赋值,如this->a = 10 为错误的 //this->c = 90 为错误 std::cout << this->a << this->b << this->c << std::endl; } constmutable(); ~constmutable(); };
转载请注明原文地址: https://www.6miu.com/read-2625795.html

最新回复(0)