1

The old way: constructor function is the same as the contract name:

contract CtrtFactory {
  function makeCtrt() {
    address ctrtAddress = new Ctrt(...);
  }
}
contract Ctrt() {
  function Ctrt(..) {}
}

-----------------== But now the new Solidity syntax is saying to use "constructor" as the function name for constructors...: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.

contract CtrtFactory {
  Ctrt ctrt;
  function makeCtrt() {
    address ctrtAddress = Ctrt.constructor(...);// This gives me ERROR...
  //but I don't know how to fix this.. adding "new" keyword does not help...
  }
}
contract Ctrt() {
  function constructor(..) {}
}

-----------------== Please help me. Thank you.

1

In Remix, this compiles:

contract CtrtFactory {

    Ctrt ctrt;

    function makeCtrt() {
        Ctrt newCtrt = new Ctrt();
        emit CtrtCreated(newCtrt);
    }

    event CtrtCreated(Ctrt ctrt);
}

contract Ctrt {
    function constructor() {}
}

Note that Ctrt's contrstructor is indeed called constructor, but we call it using new Ctrt()

  • Wow. Thank you. I think my Visual Studio Code's linter plugin has problems of not using the latest compiler version. Thanks – Russo Apr 19 '18 at 12:01

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.