Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)? I am a bit confused... and will be happy for explanation.

Thanks.

like image 662
user1673206 Avatar asked Jan 04 '15 15:01

user1673206


People also ask

Can we use polymorphism in multiple inheritance?

Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance. Whereas it can be compiled-time polymorphism (overload) as well as run-time polymorphism (overriding).

Can we achieve inheritance with polymorphism?

Real polymorphism in General can not be acheived without inheritance. Languages that are not object-oriented provide forms of polymorphism which do not rely on inheritance (i.e parametric polymorphism). Some of this is possible in Java through generics.

What is polymorphism inheritance?

Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class). Whereas polymorphism is that which can be defined in multiple forms. 2.

Can protected members be inheritance in multilevel?

Rules of inheritance - When a class is publicly inherited by another class, its public members become the public members of the derived class, its protected members become the protected members of the derived class, while the private members of any class cannot be inherited.


2 Answers

When C++ permits access to protected members, it's only to the members of this object (as mentioned here and here). The code x = p_arr[i]->function() tries to call a method in another object, so the compiler complains.

To fix your code, you can make function public, or add a friend declaration to baseClass, like this:

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:

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. 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]);
    ...
}
like image 175
anatolyg Avatar answered Sep 26 '22 20:09

anatolyg


In this case your function() is private in derived classes. So, from derived_C you cannot directly access that function.

However, if you are willing to make it public/protected. Then you can use:-

derived_C dc;
dc.derived_A::function();
dc.derived_B::function();
like image 26
ravi Avatar answered Sep 26 '22 20:09

ravi