简体   繁体   English

为什么我不能使用受保护/私有继承在派生实例中访问基类的受保护成员?

[英]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都是protectedDerived 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继承的Baseprotected继承时,如下所示

class Derived: protected Base

You are basically making any public methods of the Base class protected members of the derived class. 您基本上是在派生类的protected Baseprotected成员的所有公共方法。 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.

相关问题 受保护的继承是否允许派生类访问其基类的私有成员? - Does protected inheritance allow the derived class access the private members of its base class? 为什么派生类无法访问受保护的基类成员? - Why the derived class cannot access protected base class members? 我可以从派生类中的静态函数访问基类受保护的成员吗? - Can I access a base classes protected members from a static function in a derived class? 派生类无法访问基类的受保护方法 - Derived class can't access protected method of base class 无法从派生类访问基类中的受保护成员 - Can't access protected member in base class from derived class 为什么我只能在子类(派生类)的方法中访问 class 的受保护成员? - Why can i only access protected members of a class inside a method of a child(derived class)? 使用指向派生类中基类的指针访问受基类保护的成员 - Access base class protected members using pointers to base class in derived class 从派生类访问基类的受保护数据成员 - Access protected data members of the base class from the derived class 无法访问派生类中的基类保护成员! (在虚函数中) - Not able to access base protected members in derived class ! ( in virtual functions ) 为什么我的对象不能访问公共基类中定义的另一个对象的受保护成员? - Why can't my object access protected members of another object defined in common base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM