What are the rules for calling the superclass constructor?

前端 未结 10 2175
甜味超标
甜味超标 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:00

    Everybody mentioned a constructor call through an initialization list, but nobody said that a parent class's constructor can be called explicitly from the derived member's constructor's body. See the question Calling a constructor of the base class from a subclass' constructor body, for example. The point is that if you use an explicit call to a parent class or super class constructor in the body of a derived class, this is actually just creating an instance of the parent class and it is not invoking the parent class constructor on the derived object. The only way to invoke a parent class or super class constructor on a derived class' object is through the initialization list and not in the derived class constructor body. So maybe it should not be called a "superclass constructor call". I put this answer here because somebody might get confused (as I did).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 00:09

    In C++, the no-argument constructors for all superclasses and member variables are called for you, before entering your constructor. If you want to pass them arguments, there is a separate syntax for this called "constructor chaining", which looks like this:

    class Sub : public Base
    {
      Sub(int x, int y)
      : Base(x), member(y)
      {
      }
      Type member;
    };
    

    If anything run at this point throws, the bases/members which had previously completed construction have their destructors called and the exception is rethrown to to the caller. If you want to catch exceptions during chaining, you must use a function try block:

    class Sub : public Base
    {
      Sub(int x, int y)
      try : Base(x), member(y)
      {
        // function body goes here
      } catch(const ExceptionType &e) {
        throw kaboom();
      }
      Type member;
    };
    

    In this form, note that the try block is the body of the function, rather than being inside the body of the function; this allows it to catch exceptions thrown by implicit or explicit member and base class initializations, as well as during the body of the function. However, if a function catch block does not throw a different exception, the runtime will rethrow the original error; exceptions during initialization cannot be ignored.

    0 讨论(0)
  • 2020-11-22 00:11

    Nobody mentioned the sequence of constructor calls when a class derives from multiple classes. The sequence is as mentioned while deriving the classes.

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