Home >Backend Development >C++ >How Do I Call Base Class Constructors in C ?
Inheriting classes in Java involves invoking the parent class constructor using the super() keyword. This practice ensures that the base class's constructor is executed before the derived class's constructor.
In C , a similar mechanism exists to call base class constructors with arguments. However, it requires using the initializer list in the constructor of the derived class. Here's how it works:
class BaseClass { public: BaseClass(char *name); ... }; class DerivedClass : public BaseClass { public: DerivedClass() : BaseClass("asdf") {} };
In the example above, the DerivedClass constructor initializes the BaseClass constructor with "asdf" as the argument. This must be done before any members of the DerivedClass are initialized.
The above is the detailed content of How Do I Call Base Class Constructors in C ?. For more information, please follow other related articles on the PHP Chinese website!