Created
May 28, 2016 11:30
-
-
Save Neurone/d36c09d53ee5a9d1802dafe04d0bf0e0 to your computer and use it in GitHub Desktop.
This gist explains some misunderstandings about Solidity constructors parameters and inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This gist explains some misunderstandings about Solidity constructors parameters and inheritance | |
// Base contract to extend | |
contract BaseContract { | |
function BaseContract(bool myVar) {} | |
} | |
// Wrong way to extend a contract that use parameters in its own constructor | |
contract ExtendedContract_WrongConstructor is BaseContract { | |
bool _myVar; | |
function ExtendedContract_WrongConstructor(bool myVar) { | |
_myVar = myVar; | |
} | |
} | |
// Right way to extend a contract that use parameters in its own constructor | |
contract ExtendedContract_CorrectConstructor is BaseContract { | |
bool _myVar; | |
function ExtendedContract_CorrectConstructor(bool myVar) BaseContract(myVar){ | |
_myVar = myVar; | |
} | |
} | |
// Test the two cases | |
contract Tester { | |
function Tester(){ | |
//This does not compile | |
//ExtendedContract_WrongConstructor myContract = new ExtendedContract_WrongConstructor(true); //This throws: "Error: Trying to create an instance of an abstract contract." | |
//This compiles | |
ExtendedContract_CorrectConstructor myCorrectContract = new ExtendedContract_CorrectConstructor(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment