1

I want to use a contract inherit from another contract and use the parent constructor after using another commands in inherited constructor. So I wrote:

pragma solidity ^0.5.10;

contract A {
    constructor(uint x) public {
        //some commands
    }
}
contract B is A {
    contructor(uint x) public {
        //another commands that must be executed first

        //calling A constructor with x parameter:
        A(x);
        //or:
        super.constructor(x); //has error
        //or ???
    }
}

If I use the first solution it assumes that it should cast x variable as A contract. another solutions result error.

If we use A(x) as the B constructor modifier there is no way to run the parent commands after the inherited commands.

    ...
    contructor(uint x) public A(x) {
        //another commands that must be executed first
    }
    ...

finally I was forced to use a secondary function as follows:

pragma solidity ^0.5.10;

contract A {
    constructor(uint x) public {
        p(x);
    }
    function p(uint x) internal {
        //some commands
    }
}
contract B is A {
    contructor(uint x) public {
        //another commands that must be executed first

        //calling A constructor with x parameter:
        A.p(x);
        //or:
        super.p(x);
    }
}

Is there any straight solution, so we don't need to use the secondary function?

9
  • 1
    In contract B, you need contructor(uint x) A(x) {...}, and that's it. Sep 21 '20 at 20:38
  • And BTW, you need to declare each one of the constructors either public or internal. Sep 21 '20 at 20:39
  • P.S.: Your question is a total mess, and it is rather unclear what you want to ask. For example, it starts with a broken piece of code followed by "If I use the first solution"... What "first solution"? Where exactly is there a list of solutions prior to this statement??? Sep 21 '20 at 20:41
  • Note that we need some codes executed before the parent constructor code. Sep 21 '20 at 20:46
  • The first solution is A(x); and the second is super.constructor(x) that generates error. Sep 21 '20 at 20:47
0

Having contract A

pragma solidity ^0.5.0;

contract A {
    uint public a;
    constructor(uint _a) internal {
        a = _a;
    }
}

...

you can either pass a constant argument with this syntax

...

contract B is A(1) {
    constructor() public {}
}

or you can pass a variable with this syntax.

...

contract B is A {
    constructor(uint _y) A(_y * _y) public {}
}
4
  • This is not applicable because I want to use the argument of B constructor when I want to call A constructor, not a constant number like 1. Sep 24 '20 at 7:52
  • @AlirezaZojaji Updated the answer which takes a variable parameter.
    – ferit
    Sep 24 '20 at 8:10
  • Now the main problem is that how I can run the parent constructor after running some commands in current constructor. Sep 26 '20 at 6:20
  • That's impossible and doesn't make sense. Constructors are only for object construction. You can separate the logic you want to call into another function.
    – ferit
    Sep 26 '20 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.