What are the rules for calling the superclass constructor?

前端 未结 10 2179
甜味超标
甜味超标 2020-11-21 23:18

What are the C++ rules for calling the superclass constructor from a subclass one?

For example, I know in Java, you must do it as the first line of the subclass cons

10条回答
  •  名媛妹妹
    2020-11-22 00:04

    The only way to pass values to a parent constructor is through an initialization list. The initilization list is implemented with a : and then a list of classes and the values to be passed to that classes constructor.

    Class2::Class2(string id) : Class1(id) {
    ....
    }
    

    Also remember that if you have a constructor that takes no parameters on the parent class, it will be called automatically prior to the child constructor executing.

提交回复
热议问题