简体   繁体   English

具有多态性的多重保护继承

[英]multiple protected inheritance with polymorphism

I have a question about multiple inheritance of protected function and polymorphism. 我有一个关于受保护函数和多态的多重继承的问题。 It's quite hard to describe it so I hope it will be clear enough. 描述它很难,所以我希望它足够清楚。

Say I have three classes: 说我有三个班:

class baseClass
{
 protected:
    virtual int function() = 0;
}; 


class derived_A:public baseClass
{
  int function()
    {
      //implementation 1
    };
};


class derived_B:public baseClass
{
   int function()
     {
       //implementation 2
     };
 };


class derived_C:public derived_A, public derived_B
{
  baseClass ** p_arr; //array of pointers of baseClass kind (polymorphism)
  int x=0;

  for (int i=0; i<arraySize; i++) // array size = many classes like derived_A, derived_B...
  {
     x = p_arr[i]->function(); //I already have function that builds this array
                               //it is not the question so I didn't put it here.
     // process x
   }

};

Finally my question is - how can I access that "protected" function() from derived_C class (inside the for loop)? 最后我的问题是 - 如何从derived_C类(for循环内function()访问“protected” function() )? I am a bit confused... and will be happy for explanation. 我有点困惑......并且很乐意解释。

Thanks. 谢谢。

When C++ permits access to protected members, it's only to the members of this object (as mentioned here and here ). 当C ++允许访问protected成员时,它只对该对象的成员(如此此处所述 )。 The code x = p_arr[i]->function() tries to call a method in another object, so the compiler complains. 代码x = p_arr[i]->function()尝试调用另一个对象中的方法,因此编译器会抱怨。

To fix your code, you can make function public, or add a friend declaration to baseClass , like this: 要修复代码,可以将function public,或者向baseClass添加一个friend声明,如下所示:

class baseClass
{
 public:
    virtual int function() = 0;
}; 

Or 要么

class baseClass
{
 protected:
    friend class derived_C;
    virtual int function() = 0;
}; 

However, to retain the protected access and not mention the name of the derived class in the base class, you can fix your code by adding a static accessor function to the base class: 但是,要保留protected访问权限而不提及基类中派生类的名称,可以通过向基类添加static访问器函数来修复代码:

class baseClass
{
 protected:
    virtual int function() = 0;
    static int call_the_function_on_object(baseClass& obj) {return obj.function();}
}; 

Use it (in a derived class) this way: 以这种方式使用它(在派生类中):

x = call_the_function_on_object(*p_arr[i]);

You can also give the accessor function the same name, but then, if your derived_C overrides the virtual method, it will hide the accessor function. 您也可以为访问者函数指定相同的名称,但是,如果您的derived_C重写了虚方法,它将隐藏访问者函数。 You can fix that by referring to the base class explicitly: 您可以通过显式引用基类来解决这个问题:

class baseClass
{
 protected:
    virtual int function() = 0;
    static int function(baseClass& obj) {return obj.function();}
}; 

...
class derived_C:public derived_A, public derived_B
{
    ...
        x = baseClass::function(*p_arr[i]);
    ...
}

In this case your function() is private in derived classes. 在这种情况下,您的function()在派生类中是私有的。 So, from derived_C you cannot directly access that function. 因此,从derived_C您无法直接访问该函数。

However, if you are willing to make it public/protected . 但是,如果您愿意将其public/protected Then you can use:- 然后你可以使用: -

derived_C dc;
dc.derived_A::function();
dc.derived_B::function();

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

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