Suppose I have the following class:
class Test
{
int num;
public:
Test(int x):num(x){}
Test(const Test &rhs):num(rhs.num+1){}
};
int main()
{
Test test(10);
Test copy = test;
}
The num
in the copy should be 11
, and my question is about inside the copy constructor, why can we access the private member num
of test
using num
to initialize the num
in the copy? What is confusing me is that if you type cout<<test.num<<endl
, of course it's wrong because you are trying to access the private num
, but if you pass the test by reference to the copy constructor, it works, can anybody tell me what is going on here?