C++ Inheritance: Calling Base Class Constructor In Header

后端 未结 2 739
孤街浪徒
孤街浪徒 2021-01-04 11:16

Assume class Child is a derived class of the class Parent. In a five file program, how would I specify in Child.h that I want to call

相关标签:
2条回答
  • 2021-01-04 11:30

    In Child.h, you would simply declare:

    Child(int Param, int ParamTwo);
    

    In Child.cpp, you would then have:

    Child::Child(int Param, int ParamTwo) : Parent(Param) {
        //rest of constructor here
    }
    
    0 讨论(0)
  • 2021-01-04 11:41

    The initialization list of a constructor is part of its definition. You can either define it inline in your class declaration

    class Child : public Parent {
        // ...
        Child(int Param, int ParamTwo) : Parent(Param)
        { /* Note the body */ }
    };
    

    or just declare it

    class Child : public Parent {
        // ...
        Child(int Param, int ParamTwo);
    };
    

    and define in the compilation unit (Child.cpp)

    Child::Child(int Param, int ParamTwo) : Parent(Param) {
    }
    
    0 讨论(0)
提交回复
热议问题