一. 友元
友元关系不能继承,也就是说基类友元不能访问派生类私有和保护成员
class A
{
void a(Person p)
{
cout << p.id << endl;
}
void b(Student s) //错误做法,A不能访问Student的隐藏成员
{
cout << s.add << endl;
}
};
class Person
{
public:
friend A;
Person(string name="kzz")
{}
private:
int id;
};
class Student : public Person
{
private:
int add;
public:
Student()
:Person("zhang")
{}
};
二. 静态成员
基类定义了static静态成员,则整个继承体系里面只有⼀个这样的成员。⽆论派⽣出多少个派⽣类,都 只有⼀个static成员实例。
class Person
{
public:
static int id;
};
int Person::id = 1;
class Student : public Person
{};
int main()
{
Student stu;
Person p;
cout << &p.id << endl;
cout << &stu.id << endl;
return 0;
}
基类和子类的静态成员地址是一样的,说明只有这一个静态变量