inheritance

I want a vector of derived class pointers as base class pointers

大城市里の小女人 提交于 2019-12-28 04:23:10
问题 In C++, the vector class stores an array of objects. In this case, I am storing pointers to derived class objects (Dogs). At some point, I want to treat this vector as pointers to objects of the base class (Animals). This is the "right"/non controversial way right? Why can't I do this? #include <vector> using namespace std; class Animal { }; class Dog : public Animal { }; int main(int argc, char *argv[]) { vector<Dog*> dogs; dogs.push_back(new Dog()); dogs.push_back(new Dog()); vector<Animal*

What's the best way to ensure a base class's static constructor is called?

陌路散爱 提交于 2019-12-28 03:41:25
问题 The documentation on static constructors in C# says: A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced . That last part (about when it is automatically called) threw me for a loop; until reading that part I thought that by simply accessing a class in any way , I could be sure that its base class's static constructor

Javascript “OOP” and prototypes with multiple-level inheritance

て烟熏妆下的殇ゞ 提交于 2019-12-28 03:32:10
问题 I'm new to Javascript programming and I'm approaching my first application (a game, indeed) from an object oriented programming perspective (I know js is not really object oriented, but for this particular problem it was easier for me to start like this). I have a hierarchy of "classes" where the top-most ("Thing" class) defines a list of related things (attached items in the game). It's inherited by a ThingA class which is inherited by ThingA1 and ThingA2 classes. The minimal example would

Two methods inherited from one method in class are different in instances, aren't they?

早过忘川 提交于 2019-12-28 03:11:22
问题 Can someone please explain why it is so? class Foo: def bar(self): pass a = Foo() b = Foo() a.bar == b.bar # False a.bar is b.bar # False I thought that they both inherit the class method and it's the one method. 回答1: When you access a function through an instance that is defined on the class, a bound-method object is created each time. From the docs: What exactly happens when a method is called? You may have noticed that x.f() was called without an argument above, even though the function

Why must virtual base classes be constructed by the most derived class?

本小妞迷上赌 提交于 2019-12-28 03:05:25
问题 The following code won't compile: class A { public: A(int) {} }; class B: virtual public A { public: B(): A(0) {} }; // most derived class class C: public B { public: C() {} // wrong!!! }; If I call A 's constructor in C 's constructor initialization list, that is: // most derived class class C: public B { public: C(): A(0) {} // OK!!! }; it does work. Apparently, the reason is because virtual base classes must always be constructed by the most derived classes . I don't understand the reason

Why must virtual base classes be constructed by the most derived class?

强颜欢笑 提交于 2019-12-28 03:05:22
问题 The following code won't compile: class A { public: A(int) {} }; class B: virtual public A { public: B(): A(0) {} }; // most derived class class C: public B { public: C() {} // wrong!!! }; If I call A 's constructor in C 's constructor initialization list, that is: // most derived class class C: public B { public: C(): A(0) {} // OK!!! }; it does work. Apparently, the reason is because virtual base classes must always be constructed by the most derived classes . I don't understand the reason

Why is super class constructor always called [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-28 03:04:29
问题 This question already has answers here : Why do this() and super() have to be the first statement in a constructor? (19 answers) Closed 3 years ago . I have the following 2 classes public class classA { classA() { System.out.println("A"); } } class classB extends classA { classB() { System.out.println("B"); } } and then running 1 classA c = new classB(); or 2 classB c = new classB(); always gives A B Why is this happening? At first glance, in either scenario, I would assume that only the

Python: Inherit the superclass __init__

放肆的年华 提交于 2019-12-28 02:37:06
问题 I have a base class with a lot of __init__ arguments: def BaseClass(object): def __init__(self, a, b, c, d, e, f, ...): self._a=a+b self._b=b if b else a ... All the inheriting classes should run __init__ method of the base class. I can write a __init__() method in each of the inheriting classes that would call the superclass __init__ , but that would be a serious code duplication: def A(BaseClass): def __init__(self, a, b, c, d, e, f, ...): super(A, self).__init__(a, b, c, d, e, f, ...) def

how to inherit Constructor from super class to sub class

巧了我就是萌 提交于 2019-12-28 02:05:08
问题 How to inherit the constructor from a super class to a sub class? 回答1: Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass. Here is an example of how this works: class Foo { Foo(String str) { } } class Bar extends Foo { Bar(String str) { // Here I am explicitly calling the superclass // constructor - since constructors are not inherited // you must chain them like this. super(str); }

how to inherit Constructor from super class to sub class

ε祈祈猫儿з 提交于 2019-12-28 02:04:12
问题 How to inherit the constructor from a super class to a sub class? 回答1: Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass. Here is an example of how this works: class Foo { Foo(String str) { } } class Bar extends Foo { Bar(String str) { // Here I am explicitly calling the superclass // constructor - since constructors are not inherited // you must chain them like this. super(str); }