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?
contract B
, you needcontructor(uint x) A(x) {...}
, and that's it.public
orinternal
.A(x);
and the second issuper.constructor(x)
that generates error.