mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add example of constructor inheritance and order
Add an example of how contract inheritance impacts constructor calling
This commit is contained in:
parent
04bad01ab1
commit
d7fe96f81f
@ -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
|
requests to override ``X``, which is a contradiction that
|
||||||
cannot be resolved.
|
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
|
Inheriting Different Kinds of Members of the Same Name
|
||||||
|
Loading…
Reference in New Issue
Block a user