多路径继承和
Multipath Inheritance and
在以下代码中,使用Virtual Class
解析Multi Path Inheritance
建造师是如何工作的?构造函数不能是继承的、虚拟的或静态的。
/*Multi Path Inheritance*/
class A{
public:
int a;
A(){
a=50;
}
};
class B:virtual public A{
public:
/*B(){
a = 40;
}*/
};
class C:virtual public A{
public:
/*C(){
a = 30;
}*/
};
class E:virtual public A{
public:
E(){
a = 40;
}
};
class D : public B, public C, public E{
public:
D(){
cout<<"The value of a is : "<<a<<endl;
}
};
int main(int argc, char *argv[]){
D d;
return 0;
}
基于标准12.6.2/10中的以下配额,因此构造函数主体将按以下顺序调用:A->B->C->D、 因此a的最终值将是40。
在非委托构造函数中,初始化在以下顺序:
— First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list. — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
你可以在这里找到很多关于虚拟继承的信息和例子(是的,它实际上在msdn上,很奇怪:)
至于构造函数,构造函数在指定时被调用。如果您没有指定对虚拟基类构造函数的调用
类中任意位置的虚拟基类的构造函数继承层次结构由"最派生"类的构造函数。
(请在此处阅读)。
相关文章:
- 如何将更多文件夹添加到c++include路径
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++:如何为多个重载函数保留通用代码路径?
- 当依赖关系和依赖关系都是多态时,在哪个继承级别存储依赖关系指针?
- 使用多参数包可变参数模板继承类
- C++:对于继承多个类的类,如何从一个类继承某些属性,从另一个类继承其他属性?
- 在继承多个空类时,了解GCC/Clang vs MSVC2015之间的不同填充规则
- 继承多态性功能调用
- 收到未定义的引用错误 c++ 继承/多态性
- C++多重继承多态性
- 虚拟继承/多态性
- 多级继承/多态性和虚函数
- 寻找继承/多态性替代方案的任何理由
- 继承/多态性-调用子类的方法
- Visual Studio 2010 C++是否可以从引用的项目继承include路径
- 多路径继承和
- 如何找出变量的类型,包括继承/多态性
- 多重继承/多态作业
- 消息映射MFC:继承多个消息映射
- 如何使用可变模板来继承多个接口