From d7fe96f81f274083e0d350dd961423fb4a651c86 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 9 Jul 2019 11:37:25 -0400 Subject: [PATCH] Add example of constructor inheritance and order Add an example of how contract inheritance impacts constructor calling --- docs/contracts/inheritance.rst | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/contracts/inheritance.rst b/docs/contracts/inheritance.rst index 6f97f8719..28df381ee 100644 --- a/docs/contracts/inheritance.rst +++ b/docs/contracts/inheritance.rst @@ -297,6 +297,43 @@ The reason for this is that ``C`` requests ``X`` to override ``A`` requests to override ``X``, which is a contradiction that cannot be resolved. +One area where inheritance linearization is especially important and perhaps not as clear is when there are multiple constructors in the inheritance hierarchy. The constructors will always be executed in the linearized order, regardless of the order in which their arguments are provided in the inheriting contract's constructor. For example: + +:: + + pragma solidity >=0.4.0 <0.7.0; + + contract Base1 { + constructor() public {} + } + + contract Base2 { + constructor() public {} + } + + // Constructors are executed in the following order: + // 1 - Base1 + // 2 - Base2 + // 3 - Derived1 + contract Derived1 is Base1, Base2 { + constructor() public Base1() Base2() {} + } + + // Constructors are executed in the following order: + // 1 - Base2 + // 2 - Base1 + // 3 - Derived2 + contract Derived2 is Base2, Base1 { + constructor() public Base2() Base1() {} + } + + // Constructors are still executed in the following order: + // 1 - Base2 + // 2 - Base1 + // 3 - Derived3 + contract Derived3 is Base2, Base1 { + constructor() public Base1() Base2() {} + } Inheriting Different Kinds of Members of the Same Name