利用引用访问私有数据成员
#include<iostream>
using namespace std;
//引用访问私有数据成员
class Test
{
private:
int x,y;
public:
void setxy(int a ,int b)
{
x = a;
y = b;
}
void getxy(int &px,int &py)
{
px = x;//相当于 &px = a; px就是a了!
py = y;
}
void printxy()
{
cout << x <<endl;
cout << y <<endl;
}
};
int main()
{
Test p;
p.setxy(3,5);
int a,b;
p.getxy(a,b);
p.printxy();
return 0;
}
浙公网安备 33010602011771号