| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
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:的成员,只能被类及其子类和友元函数访问,但是不能被该类的对象访问的。 
 
 
 |   
 
 
 
 |