只有常成员函数才有资格操作常对象,
没有使用const关键字说明的成员函数不能用来操作常对象
- #include
- using namespace std;
- class r
- {
- public:
- r(int a, int b):r1(a),r2(b) {}
- void print();
- void print() const;
- private:
- int r1, r2;
- };
- void r::print()
- {
-
cout<
"," < - }
- void r::print() const
- {
-
cout<<"const: "<
"," < - }
- void main()
- {
- r a(5, 4);
- a.print();
- const r b(20, 52);
- b.print();
- }
