C++ 类的私有成员被设计为对其他类实例不可见。我很困惑,因为可以访问私人成员,如下所示!谁能给我解释一下?
这是我的代码:
#include <iostream>
using namespace std;
class Person
{
private:
char* name;
int age;
public:
Person(char* nameTemp, int ageTemp)
{
name = new char[strlen(nameTemp) + 1];
strcpy(name, nameTemp);
age = ageTemp;
}
~Person()
{
if(name != NULL)
delete[] name;
name = NULL;
}
bool Compare(Person& p)
{
//p can access the private param: p
//this is where confused me
if(this->age < p.age) return false;
return true;
}
};
int main()
{
Person p("Hello, world!", 23);
return 0;
}