C++学习5 - const成员函数

来源:互联网 发布:win7定时开关机软件 编辑:程序博客网 时间:2024/05/22 05:21

#include<iostream>
using namespace std;
class A{
public:
 void func(int x, int y){
  i = x;
  j = y;
 }
 void print()const{
  cout << "i*j="<<i*j << endl;
 }
private:
 int i, j;
};
int main(){
 A a;
 a.func(1, 2);
 a.print();
 system("pause");
 return 0;
}

 

const成员函数:如果你不想让成员函数修改成员变量的值,就可以把成员函数声明为const

 

更多详情请点击  http://blog.sina.com.cn/zhaojianjunzjj
原创粉丝点击