c++: Why the member functions of one class can access to private data member of other object? I use vs2008 and g++ compiler. the codes are list below:
#include <iostream>
using namespace std;
class Integer {
int i;
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const {
cout << "operator+" << endl;
return Integer(i + rv.i /*? rv.i access to the private data member*/);
}
Integer&
operator+=(const Integer& rv) {
cout << "operator+=" << endl;
i += rv.i; //? rv.i access to the private data member
return *this;
}
void combine( Integer& rv)
{
rv.i = 2;//?rv.i access to the private data member
}
};
int main() {
cout << "built-in types:" << endl;
int i = 1, j = 2, k = 3;
k += i + j;
cout << "user-defined types:" << endl;
Integer ii(1), jj(2), kk(3);
kk += ii + jj;
} ///:~
The private member data of other object is followed by "//?" comment lines.
I have view the thread http://www.daniweb.com/forums/thread1460.html but it seems not helpful.
Thanks in advance!