No, they are inherited. Destructors, for one, are always inherited and called in the reverse order of creation. For example, if we have the classes foo , bar and xyzzy :
class foo { ... }; class bar : public foo { ... }; class xyzzy : public bar { ... };
Then, if you destroyed an object of class xyzzy , destructors will be called in the following order: ~xyzzy() , ~bar() and finally ~foo() .
Constructors are also always inherited, but they cannot be called directly. You must use them in the constructor initialization list or call the default constructor (the default constructor is one that takes no arguments). For example, let's say that we have the following classes:
class foo { public: foo(); foo (int _value); } class bar : public foo { int m_value; public: bar (int _value); } bar::bar (int _value) { m_value = _value; }
In this case, when creating an object of class bar , the constructor for foo is called, but this is the default constructor ( foo() ). A constructor that takes an argument foo (int _value) is never called. But if we change the constructor definition of bar (int _value) to this:
bar::bar (int _value) : foo (256 - _value), m_value (_value) { }
Then, instead of the default constructor, foo (int _value) will be called.
Septagram Nov 12 2018-11-11T00
source share