同一个类的两个不同对象可以访问对方的private成员吗?
请大家看看下面这段程序,它是能够正常编译连接运行的。但是为什么
assign成员函数没有错呢?谁能告诉我c++中关于这种情况的内幕?谢谢!
#include <stdio.h>
class Test {
private:
int a;
int b;
public:
Test(int a_v, int b_v);
void assign(Test *source);
void print(void);
};
Test::Test(int a_v, int b_v)
{
a = a_v;
b = b_v;
}
void Test::assign(Test *source)
{
a = source->a;
b = source->b;
}
void Test::print()
{
printf("a = %d, b = %d\n", a, b);
}
int main()
{
Test t1(1, 2), t2(3, 4);
t2.assign(&t1);
t2.print();
return 0;
}