Possible to delay running constructor of parent contract?

Hey guys, is it possible to postpone running the constructor of parent contract to the end of executing constructor of this contract? See the following code snippet for example.

contract A {
    constructor(uint256 a, uint256 b) {}
}

contract B is A {
    constructor(uint256 a, uint256 b, uint256 c) {
        // Do Sth
        A(a, b);
    }
}

Not really. Base constructors always run first because the expectation is that the base contract is already initialized when yours runs. Otherwise it could overwrite your changes and not satisfy the postconditions it was supposed to satisfy.

That’s the same as in C++ for example.

1 Like

Actually it’s not possible directly but there is a workaround as long as it’s the constructor of the parent contract (and not one somewhere deeper in the hierarchy). You could create an extra abstract contract and inherit from it in such a way that it’s before the parent contract in the linearized order. Then the constructor of the new contract would run before the parent constructor. See Multiple Inheritance and Linearization.

1 Like

This is a very clever idea. This could help solve the problem I’m facing here. And yes, this is working. Many thanks. :+1:

1 Like