请问为啥成员函数sum中对象可以直接访问私有类型成员a.x
#include<iostream>
using namespace std;
class test
{
private:
int x;
int y;
public:
test(int a = 0, int b = 0) :x(a), y(b) {}
int getx()
{
return x;
}
int gety() { return y; }
void sum(test a)
{
cout << this->x + a.x + a.y << endl;
}
friend void Sum(test &a, test &b);
};
void Sum(test &a,test &b)
{
cout << a.x + a.y + b.x + b.y << endl;
}
int main()
{
test t(1, 2);
test b(3, 4);
t.sum(b);
Sum(t, b);
}