2

Which variables types are accepted by a constructor in Solidity? In particular, is it possible to pass an actual contract as an constructor input? For example:

contract ContractA {
    constructor (uint _id) public {} 
}


contract ContractB {

    ContractA ContractAReal;

    constructor (ContractA _metaContractA) public {
        ContractAReal = _metaContractA;
    }
}
1

I believe you can pass pretty much anything. I'm not sure if 0.5.0 uses the new ABI encoder yet, so you might not be able to pass nested variable length things (like arrays of strings).

Yes, you can take a contract as a parameter. It's really just taking an address as a parameter and then casting it to that contract type.

  • but what is the exact syntax used to set the embedded contact in the constructor, is it: contactAReal = metaContractA; OR contactAReal = contractA(msg.sender); – Yeez Dec 8 '18 at 21:50
  • What you have already is correct if you want to use the address passed in. The latter option would make sense if you were trying to use the address of a contract that created this one. – user19510 Dec 8 '18 at 22:27

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.