3 ms·
The solution in C++ isn't so great either, considering: struct Base { Base() { cout << "Base" << endl; } }; struct Derived1 : Base { Derived1() { cout <<
by vore 6y ago
The solution in C++ isn't so great either, considering:
struct Base { Base() { cout << "Base" << endl; } };
struct Derived1 : Base { Derived1() { cout << "Derived1" << endl; } };
struct Derived2 : Base { Derived2() { cout << "Derived2" << endl; } };
struct Join : Derived1, Derived2 { Join() { cout << "Join" << endl; } };
And then you get two calls to Base:
Base
Derived1
Base
Derived2
Join
There's virtual base class inheritance but then that's a different problem that's hardly any more straightforward than Python.
- mehrdadn 6y ago> And then you get two calls to Base: "2 calls to Base" is a misleading way to put it. You in fact get exactly 1 call per every instance of Base in the hierarchy (and they act independently). Contrast that with Python where you can accidentally call the same constructor multiple times for the same instance of that class, and Python doesn't care to prevent you. (And notice how others' proposed solutions in another comment did exactly that, and they didn't realize it either.) > There's virtual base class inheritance but then that's a different problem that's hardly any more straightforward than Python. This is debatable (the Python behavior is quite unintuitive) but "straightforwardness" is actually besides my point. At least with virtual base classes, the solution (whether complicated or not) is in the same place as the problem: both are in the derived class. With Python, the solution is in the base class, which another author wrote a long time ago. Whereas the problem only occurs when you, another poor soul, tries to derive from it a long time later. This kind of spooky action at a distance makes it impractical to abstract things away... in a sense it literally violates causality (for the lack of a better word)! That's not something to take lightly, to say the least.