protected的疑问
Girl作为Lover的子类,应该是能访问到Lover中的protected成员name的,但是编译器还是报被保护,无法访问,这是为什么?class Lover
{
public:
Lover(std::string theName);
protected:
std::string name;
};
Lover::Lover(std::string theName)
{
name = theName;
std::cout << name << "诞生!" << std::endl;
}
class Girl : public Lover
{
public:
Girl(std::string theName);
void cook(Lover name_lover);
};
Girl::Girl(std::string theName) : Lover(theName)
{
}
void Girl::cook(Lover name_lover)
{
std::cout << name_lover.name << std::endl;
} Protected:的成员,只能被类及其子类和友元函数访问,但是不能被该类的对象访问的。
页:
[1]