【发布时间】:2015-03-02 06:24:55
【问题描述】:
我有一个关于受保护函数和多态性的多重继承的问题。 很难描述,所以我希望它足够清楚。
假设我有三个班级:
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
}
};
最后我的问题是 - 我如何从 derived_C 类(在 for 循环内)访问“受保护的”function()?
我有点困惑......并且很乐意解释。
谢谢。
【问题讨论】:
标签: c++ inheritance polymorphism protected