7

Suppose I have the folloing:

contract Owned {
    address owner;

    function owned() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        if (msg.sender == owner) _
    }
}

contract Example is Owned {
    function doSomething() {
        if (address == <some address here) <statement>;
    }
}

The above seems to work. So, I assume 2 things:

  1. The compiler generates a default constructor for Example.
  2. The address property is default-visible to child contracts.

About 1., what would happen if the parent constructor had arguments and I didn't declare it explicitly?

Would it behave like Java classes, copying the constructor signature to it's child and, therefore, making me pass the arguments when creating the contract?

Or Would it behave like PHP (older) classes, simply not initializing the property?

About 2., I've noticed that there is a public modifier for properties, but I'm not sure of what omitting such modifier defaults to.

1 Answer 1

5

Good Question!

Lets explore the constructor inheritance:

Lets look at the following example:

import "dapple/test.sol";

contract A {
    bool aHit;
    bool public aPublicHit;
    bool public aConstructorArg;
    bool cHit;

    function A(bool _aConstructorArg) {
      aHit = true;
      aPublicHit = true;
      aConstructorArg = _aConstructorArg;
    }
}

contract B is A(true) {
    bool bHit;
    bool public bConstructorArg;
    function B(bool _bConstructorArg) {
      bHit = true;
      bConstructorArg = _bConstructorArg;
    }
    function checkA() returns (bool) {
      return aHit;
    }
    function checkB() returns (bool) {
      return bHit;
    }
    function checkC() returns (bool) {
      return cHit;
    }
}

contract T is Test {

  function testConstructor() {
    B b = new B(true);
    //@log constructor A called: `bool b.checkA()`
    //@log constructor B called: `bool b.checkB()`
    //@log controll call c: `bool b.checkC()`
    //@log public call: `bool b.aPublicHit()`
    //@log a constructor arguments recieved: `bool b.aConstructorArg()`
    //@log b constructor arguments recieved: `bool b.bConstructorArg()`
  }

}

If we now run testConstructor function with dapple test --report we will get the following output:

  test constructor
  LOG:  constructor A called: true
  LOG:  constructor B called: true
  LOG:  controll call c: false
  LOG:  public call: true
  LOG:  a constructor arguments recieved: true
  LOG:  b constructor arguments recieved: true

About 1., what would happen if the parent constructor had arguments and I didn't declare it explicitly?

In case:

contract A { function A(bool) { ... }}
contract B is A { ... }

your contract would fail to instantiate:

Error: Trying to create an instance of an abstract contract.
    B b = new B(true);
          ^---^

About 2., I've noticed that there is a public modifier for properties, but I'm not sure of what omitting such modifier defaults to.

The public keyword generates an interface to access the property from outside of the Contract. I hope the example above makes this clear.

If you want to learn more about inheritance, I recommend you reading the following links:

https://solidity.readthedocs.io/en/develop/contracts.html?highlight=inheritance#inheritance

https://solidity.readthedocs.io/en/develop/contracts.html?highlight=inheritance#arguments-for-base-constructors

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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