简体   繁体   English

C++ 多级继承,多态

[英]C++ Multilevel Inheritance, Polymorphism

Suppose i have three classes A, B and C. class B inherits from class A and the inheritance is private whereas class C inherits from B and the inheritance is public.假设我有三个类 A、B 和 C。类 B 继承自类 A 并且继承是私有的,而类 C 继承自 B 并且继承是公共的。 Now class A has a protected function which class C wants to access.现在 A 类有一个受保护的函数,C 类想要访问它。 So, what must be done in class B to make that protected function available to class C.那么,必须在 B 类中做什么才能使该受保护的函数对 C 类可用。

Here is the link to the code : http://pastebin.com/9E2sLZzj这是代码的链接: http : //pastebin.com/9E2sLZzj

The "using" keyword makes a member of an inherited class visible, and resolvable, in the scope of its subclass. “using”关键字使继承类的成员在其子类的范围内可见和可解析。 So, to make the privately-inherited member available to B 's subclasses:因此,要使私有继承的成员可用于B的子类:

class A {

protected:

    void foo() {}
};

class B : private A {

protected:

    using A::foo;
};

class C : public B {

    void bar()
    {
        foo();
    }
};

Okay i got the solution This code fragment worked after inserting it into Class B.好的,我得到了解决方案 此代码片段在将其插入 B 类后工作。

int get(){
  return A::get();
}

Not sure what it does though虽然不确定它的作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM