利用引用访问私有数据成员

#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;
}

  

posted on 2013-10-12 17:30  Lingc·  阅读(393)  评论(0编辑  收藏  举报

导航

不知道不知道 知道不知道 知道知道 不知道知道。
天道酬勤,同志们共同努力!