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

xiaoxiao2021-02-28  87

#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
转载请注明原文地址: https://www.6miu.com/read-74211.html

最新回复(0)