[英]Why can't I access protected members of base class in derived instances using protected/private inheritance?
I thought that protected members of a Base class can be accessed by instances of the Derived class (or any classes that derive from the Derived class, given that they inherit from the latter publicly). 我认为,派生类的实例(或派生于派生类的任何类,只要它们是从派生类公开继承的)都可以访问基类的受保护成员。
But I get an error when attempting to do just that in the following listings. 但是在以下清单中尝试执行此操作时出现错误。 So what am I missing?
那我想念什么呢?
class Base{
private:
virtual void f(){cout << "f of Base" << endl;}
public:
virtual ~Base(){}
virtual void g(){this->f();}
virtual void h(){cout << "h of Base "; this->f();}
};
class Derived: protected Base{
public:
virtual ~Derived(){}
virtual void f(){cout << "f of Derived" << endl;}
private:
virtual void h(){cout << "h of Derived "; this->f();}
};
int main(){
Base *base = new Base();
cout << "Base" << endl;
base->g(); //f of Base
base->h(); //h of Base f of Base
Derived *derived = new Derived();
cout << "Derived" << endl;
derived->f(); //f of Derived
derived->g(); //this doesn't compile and I get the error "void Base::g() is inaccessible within this context". Why?
derived->h(); //this doesn't compile given that h is private in Derived
delete base;
delete derived;
return EXIT_SUCCESS;
}
Since Derived
inherits from Base
protected
ly, all public members of Base
are protected
in Derived
. 由于
Derived
继承自Base
protected
LY,所有公共成员Base
都是protected
中Derived
。 This means, outside of Derived
(eg in main
), those members' name are not visible. 这意味着在“
Derived
之外(例如,在main
),这些成员的名称不可见。
[class.access.base]/1
[...] If a class is declared to be a base class for another class using the
protected
access specifier, the public and protected members of the base class are accessible as protected members of the derived class.[...]如果使用
protected
访问说明符将一个类声明为另一个类的基类,则可以将基类的公共成员和受保护成员作为派生类的受保护成员进行访问。 [...][...]
[class.access]/1.2
A member of a class can be
班级成员可以是
(1.2) protected;
(1.2)受保护; that is, its name can be used only by members and friends of the class in which it is declared, by classes derived from that class, and by their friends (see [class.protected]).
也就是说,它的名称只能由声明它的类的成员和朋友,该类派生的类及其朋友使用(请参阅[class.protected])。
derived->g();
派生-> g();
can be accessed by changing inheritance to public. 可以通过将继承更改为public来访问。
derived->h();
派生-> h();
can be accessed by changing access specifier inside derived class from private to public(still keeping inheritance as protected,since derived class pointer points to its member function) 可以通过将派生类内部的访问说明符从私有更改为公共来进行访问(由于继承类指针指向其成员函数,因此仍将继承保持为受保护状态)
When you declare protected
inheritance of Base
by Derived
as follows 当您声明由
Derived
继承的Base
的protected
继承时,如下所示
class Derived: protected Base
You are basically making any public methods of the Base
class protected
members of the derived class. 您基本上是在派生类的
protected
Base
类protected
成员的所有公共方法。 If you instead declare the inheritance as public via 如果您改为通过以下方式将继承声明为公共
class Derived: public Base
you will find you will be able to access derived->g()
just fine. 您会发现您将能够很好地访问
derived->g()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.