About base class in derived class initialization

后端 未结 5 1789
醉梦人生
醉梦人生 2021-01-24 10:41

guys. I see several cases like:

class Derived:public Base{

public:
    Derived(...):Base(...){}
}

Is what situation or is there any principle

相关标签:
5条回答
  • 2021-01-24 11:16

    If don't explitily initialize the base class, the default constructor will be called.

    0 讨论(0)
  • 2021-01-24 11:16

    A good coding standard will recommend you to always initialize base class in the constructor's initialization list.

    If the constructor of the base class requires some arguments, then you have to do it.

    0 讨论(0)
  • 2021-01-24 11:24

    In case if we need to pass the arguments of derived constructor to the base constructor, it can be used.

    class foo
    {
        public:
            foo() {} 
            foo( int num ) {}
    };
    
    class bar : public foo
    {
        public:
            bar(int barNum): foo(barNum) {}
    };
    
    0 讨论(0)
  • 2021-01-24 11:34

    If you have multiple constructor in your base class (basically entry point), So you have choice to call any of them.

    0 讨论(0)
  • 2021-01-24 11:35

    If you want to call a base constructor with arguments.

    0 讨论(0)
提交回复
热议问题