1

I was going through a code in solidity . Being a beginner I was completely flummoxed by the following line of code :

function foo()ParentToken(currentSupply,tokenName,decimals,symbol){}

Here I was unable to understand what this above line of code is intended for .

Is there any c++ analogy for this kind of construct in solidity language ?

I tried thinking about it as an overloaded () operator but could not make a fit .

Thanks in advance

2

It's a bad example of an inherited constructor. I say "bad" because naming conventions would call for a CapsCase function name that would provide more of a hint.

This function can only be explained in a wider context of a contract with inheritance. Something like:

contract ParentToken {

  function ParentToken(currentSupply, tokenName ...) {} // this is the constructor
}

contract foo is Class { // should be "Foo"

  uint currentSupply; // need to get this value from somewhere ... 

  function foo()
    ParentToken(currentSupply, tok...) { // stuff to pass to Parent's constructor
  }
}

In older versions of Solidity, the convention was a function with the same name as the contract itself was treated as a constructor. In more recent versions, this is replaced with the keyword constructor. It's less ambiguous and less error-prone.

contract ParentToken {

  constructor(currentSupply, tokenName ...) {} // this is the constructor
}

contract foo is Class { // should be "Foo"

  uint currentSupply; // need to get this value from somewhere ... 

  constructor foo()
    ParentToken(currentSupply, tok...) { // stuff to pass to Parent's constructor
  }
}

This answer may be deleted by the mods because the answer is the same as a previous question. I'm not sure that makes it a duplicate question.

Hope it helps.

  • My pleasure. Please accept the answer if it resolved the issue. – Rob Hitchens Jan 30 '19 at 16:35

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