本文介绍了虚拟继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 大家好, i我的遗产问题。 考虑以下因素: class A { public: A(int i){;} }; B级:虚拟公共A { public: B(int i):A(i ){;} }; C类:虚拟公共B { public : C(int i):B(i){;} //引发错误C2512:''A :: A'':不合适 默认构造函数可用 }; 有人能告诉我为什么会收到这个错误吗?它与B 具有来自A的虚拟继承这一事实有关。如果我使其成为非虚拟的,则错误是消失了。在这种情况下,虚拟关键字的影响是什么? 亲切的问候, 布鲁诺。 解决方案 当你使用虚拟继承时,你需要初始化每个虚拟的 $ b在最派生的类构造函数中$ b基类,因为如果不是这样的话,你可能会有多个基数进行初始化 (例如经典菱形虚拟继承示例)。所以你的C类 构造函数需要初始化A基础对象以及B基础 - B'的初始化A在创建C对象时被忽略。 Tom 谢谢。然而,这有以下含义: 请考虑一个新的场景: A级 { public: A(int i){;} }; B类:虚拟公共A { public: B(int i):A(i){;} }; C类:虚拟公共A { public: C(int i): A(i){;} }; D类:虚拟公共B,C { public: D(int i):C(i),B(i),A(i){;} }; 在这种情况下我使用相同的A实例结束了C和B. 多重继承是唯一可以看到用于 虚拟继承。 这样看着它,似乎在继承上下文中唯一使用''virtual''关键字 是说继承我的人必须初始化 我的基类太多了 这个在成员函数的上下文中,含义似乎与虚拟的含义无关。这是正确的吗? 亲切的问候, 布鲁诺。 ....当它们是D的子对象时。那是我提到的经典钻石 继承示例。当你创建一个D对象时,只使用了A的初始化。 多重继承是唯一可以看到用于 看起来像这样,似乎在继承上下文中唯一使用''虚拟''关键字就是说谁从我这里继承了初始化我的基类也是 对于单继承,是 - 虚拟继承实际上适用于多个继承已经或将来可能使用的情况。 。 标准库将它用于isosream和ostream的ios基类, 以确保iostream只有一个ios子对象(basic_前缀 为了清楚起见而已。) 这个含义似乎与成员函数的上下文中虚拟的含义无关。这是正确的吗? 是的。 Tom Hi all,i am having a problems with inheritance.consider the following:class A{public:A(int i){;}};class B: virtual public A{public:B(int i) : A(i){;}};class C: virtual public B{public:C(int i) : B(i){;} //raises error C2512: ''A::A'' : no appropriatedefault constructor available};can someone tell me why i get that error? it has to do with the fact that Bhas virtual inheritance from A. if i make that non-virtual, the error isgone. what is the impact of the ''virtual'' keyword in this situation?kind regards,Bruno. 解决方案When you use virtual inheritence, you need to initialize each virtualbase class in the most derived class constructor, since you might havemultiple bases that would do the initialisation if this wasn''t the case(e.g. the classic diamond virtual inheritance example). So your C classconstructor needs to initialize the A base object as well as the B one -B''s initialization of A is ignored when creating a C object.Tomthanks. however, this has the followig implications:please consider a new scenario:class A{public:A(int i){;}};class B: virtual public A{public:B(int i) : A(i){;}};class C: virtual public A{public:C(int i) : A(i){;}};class D: virtual public B, C{public:D(int i): C(i),B(i),A(i){;}};in this case i end up with C and B using the same instance of A.multiple inheritance is the only scenario in which i can see a use forvirtual inheritance.looking at it like this, it seems that the only use of the ''virtual'' keywordin inheritance context is to say "whoever inherits from me has to initializemy base class too"this meaning seems unrelated to the meaning of virtual in the context ofmember functions. is that correct?kind regards,Bruno..... when they are subobjects of a D. That''s the classic diamondinheritance example I mentioned. When you create a D object, only D''sinitialization of A is used. multiple inheritance is the only scenario in which i can see a use for virtual inheritance. looking at it like this, it seems that the only use of the ''virtual'' keyword in inheritance context is to say "whoever inherits from me has to initialize my base class too"For single inheritance, yes - virtual inheritance is really for caseswhere multiple inheritance is, or may be in the future, employed. Thestandard library uses it for the ios base class of istream and ostream,to ensure that iostream only has one ios subobject (basic_ prefixeselided for clarity). this meaning seems unrelated to the meaning of virtual in the context of member functions. is that correct?Yes.Tom 这篇关于虚拟继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 08:34