【发布时间】:2016-02-14 23:34:39
【问题描述】:
如果我有此代码,例如:
class ChildOne
{
ChildOneFunc()
{
Parent::ParentFunction();
}
CallNeighbour()
{
ChildTwoFunc();
}
};
class ChildTwo
{
ChildTwoFunc();
};
class Parent : public virtual ChildOne, public virtual ChildTwo
{
Parent() //Constructor
{
ChildOneFunc();
}
ParentFunction();
};
怎么可能做到这一点? 因为在 Child 2 中,Class Parent 没有定义和前向声明 只给我错误。
第二个问题:
如何从 Childtwo 调用 CildOneFunction()?
【问题讨论】:
-
通常,派生类称为“子类”,基类称为“父类”。你把它颠倒过来,这真的很混乱。
-
哦,我以为我将子类继承给父类: class Parent : public virtual ChildOne,public virtual ChildTwo 不是这样吗?
-
你永远不会继承 to (不是在现实生活中也不是在 C++ 中),但总是 from (inherit='erben' 在德语中,不是'vererben')
-
@PeterLustig,在您的代码示例中,
ChildOne和ChildTwo没有链接,而Parent是它们的派生类。比如说,ChildOne是“机械”,ChildTwo是“轮式”,Parent可以是“汽车”
标签: c++ function oop inheritance parent-child