sem*_*maj 6 c++ methods constructor base
class Base {
public:
Base() {}
void Foo(int x) {...}
};
class Derived : public Base {
public:
Derived(int args) {
/* process args in some way */
Foo(result);
}
};
Run Code Online (Sandbox Code Playgroud)
Is it allowed to call a method of the base class in the constructor of the derived class? I would imagine this is fine as the Base object should be fully constructed, but I wanted to check just in case.
sbi*_*sbi 12
Is it allowed to call a method of the base class in the constructor of the derived class?
Yes. Just watch out for virtual functions. If an class derived from Derived overrides a virtual function, while constructing Derived as a sub-object of that further derived class, the dynamic type always is Derived, so no virtual functions overridden in further derived classes are called. (The same goes for the destructor, BTW.)
我认为这很好,因为Base对象应该完全构造,但我想检查以防万一.
你的推理是正确的.