multiple-inheritance

Question on multiple inheritance, virtual base classes, and object size in C++

穿精又带淫゛_ 提交于 2019-11-29 17:43:10
问题 The following code prints 20, i.e. sizeof(z) is 20. #include <iostream.h> class Base { public: int a; }; class X:virtual public Base { public: int x; }; class Y:virtual public Base { public: int y; }; class Z:public X,public Y { }; int main() { Z z; cout << sizeof(z) <<endl; } Whereas if I don't use virtual base classes here, i.e. for the following code : sizeof(z) is 16. #include <iostream.h> class Base { public: int a; }; class X:public Base { public: int x; }; class Y:public Base { public:

Ambiguous injected class name is not an error

拜拜、爱过 提交于 2019-11-29 16:39:49
问题 What I read in the C++ standard about injected class names contradicts (as I see it) with the behavior of a sample program I will present shortly. Here's what I read: From 3.4 (paragraph 3) The injected-class-name of a class (clause 9) is also considered to be a member of that class for the purposes of name hiding and lookup. From 9 (paragraph 2) A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the

“import” a definition of a function from a base class to implement abstract interface (multiple inheritance in C++)

主宰稳场 提交于 2019-11-29 15:30:12
Say we have a class inheriting from two base classes (multiple inheritance). Base class A is abstract, declaring a pure virtual function foo , the other base class B declares and implements a function foo of the very same signature. struct A { virtual void foo(int i) = 0; }; struct B { virtual void foo(int i) {} }; struct C : public A, public B {}; I want to use the implementation of foo from base class B in my derived class C . However, if I do not implement the function foo a second time in my derived class C , I cannot instantiate any object of it (it remains abstract). Virtual inheritance

C++ Multiple Virtual Inheritance vs. COM

家住魔仙堡 提交于 2019-11-29 14:50:03
问题 The net is overflowing with explanations of the "dreaded diamond problem". So is StackOverflow. I think I understand that bit, but I fail to translate that knowledge into comprehending something similar yet different. My question begins as a pure C++ question, but the answer might well branch over into MS-COM specifics. The general problem question goes: class Base { /* pure virtual stuff */ }; class Der1 : Base /* Non-virtual! */ { /* pure virtual stuff */ }; class Der2 : Base /* Non-virtual

Multiple Inheritance: same variable name

核能气质少年 提交于 2019-11-29 13:24:09
class A { protected: string word; }; class B { protected: string word; }; class Derived: public A, public B { }; How would the accessibility of the variable word be affected in Derived ? How would I resolve it? It will be ambiguous, and you'll get a compilation error saying that. You'll need to use the right scope to use it: class Derived: public A, public B { Derived() { A::word = "A!"; B::word = "B!!"; } }; You can use the using keyword to tell the compiler which version to use: class Derived : public A, public B { protected: using A::word; }; This tells the compiler that the Derived class

In an abstract class constructor, why I do need to call a constructor of a virtual base that will never to called?

戏子无情 提交于 2019-11-29 13:05:28
I face the well known "dreaded" diamond situation : A / \ B1 B2 \ / C | D The class A has, say the constructor A::A(int i) . I also want to forbid a default instantiation of a A so I declare the default constructor of A as private . The classes B1 and B2 are virtually derived from A and have some constructors and a protected default constructor. [edit] The constructors of B1 and B2 don't call the default constructor of A . [reedit] The default constructors of B1 and B2 don't call the default constructor of A either. [reedit] [edit] The class C is an abstract class and has some constructors

How to use namedtuples in multiple inheritance

喜欢而已 提交于 2019-11-29 12:46:42
Is it possible to create a class that inherits from multiple instances of namedtuple , or create something to the same effect (having an immutable type that combines the fields of the base types)? I haven't found a way to do so. This example illustrates the problem: >>> class Test(namedtuple('One', 'foo'), namedtuple('Two', 'bar')): >>> pass >>> t = Test(1, 2) TypeError: __new__() takes 2 positional arguments but 3 were given >>> t = Test(1) >>> t.foo 1 >>> t.bar 1 The problem seems to be that namedtuple does not use super to initialize its base class, as can be seen when creating one: >>>

javascript inheritance from multiple objects

蹲街弑〆低调 提交于 2019-11-29 12:45:21
I'm not very well aquainted with javascript inheritance, and I'm trying to make one object inherit from another, and define its own methods: function Foo() {} Foo.prototype = { getColor: function () {return this.color;}, }; function FooB() {} FooB.prototype = new Foo(); FooB.prototype = { /* other methods here */ }; var x = new FooB().getColor(); However, the second one overwrites the first one( FooB.prototype = new Foo() is cancelled out ). Is there any way to fix this problem, or am I going in the wrong direction? Thanks in advance, sorry for any bad terminology. Each object can only have

pyqt5 and multiple inheritance

无人久伴 提交于 2019-11-29 11:53:28
I'd like to create a new class that inherits two subclasses of QWidget. I know multi-inheritance isn't possible in pyqt, but how could I manage to have the properties of both parent classes in one subclass? What I wish I could do is as follows: class A(QWidget): def __init__(self, widget, parent=None): widget.destroyed.connect(self.destroy_handler) @pyqtSlot() def destroy_handler(self): pass class B (A, QStatusBar): def __init__(self, widget, parent=None): A.__init__(self, widget) QStatusBar.__init__(self, parent) @pyqtSlot() def destroyed_handler(self): print("Destroyed") I finally found how

Sqlalchemy: avoiding multiple inheritance and having abstract base class

泪湿孤枕 提交于 2019-11-29 11:38:58
问题 So I have a bunch of tables using SQLAlchemy that are modelled as objects which inherit from the result to a call to declarative_base() . Ie: Base = declarative_base() class Table1(Base): # __tablename__ & such here class Table2(Base): # __tablename__ & such here Etc. I then wanted to have some common functionality available to each of my DB table classes, the easiest way to do this according to the docs is to just do multiple inheritance: Base = declarative_base() class CommonRoutines(object