需求:
我要实现一个常量字段,只能赋值一次,赋值后不容许更改。
类似于c#的readonly或者java final

#include <iostream>


class  A{
public:
A(int machine_id):_machine_id(machine_id) {
    std::cout << "构造函数"  << std::endl;
}

int get_machine_id(){
    return _machine_id;
}


//int change_machine_id(int a){
 //   _machine_id = a; //这个是不允许的
//}

private:
    const int _machine_id;
};



using namespace std;

int main()
{
    A a(100);
    std::cout << a.get_machine_id() << std::endl;
    return 0;

}

 

相关文章:

  • 2022-03-01
  • 2021-11-28
  • 2021-05-16
  • 2021-07-09
  • 2021-07-03
  • 2022-12-23
猜你喜欢
  • 2021-06-05
  • 2021-05-15
  • 2022-02-07
  • 2021-12-01
  • 2021-10-17
  • 2021-06-05
  • 2021-07-12
相关资源
相似解决方案