github.com/annchain/OG@v0.0.9/vm/vm_test/contracts/create_in_contract.sol (about)

     1  pragma solidity 0.4.20;
     2  
     3  /*
     4  * @title Example the for Solidity Course
     5  * @author Ethereum Community
     6  */
     7  
     8  // A contract can create a new contract using the new keyword.
     9  // The full code of the contract being created has to be known in advance,
    10  // so recursive creation-dependencies are not possible.
    11  
    12  // lets define a simple contract D with payable modifier
    13  contract D {
    14      uint x;
    15  
    16      function D(uint a) public payable {
    17          x = a;
    18      }
    19  
    20      function getD() public view returns (uint){
    21          return x;
    22      }
    23  }
    24  
    25  
    26  contract C {
    27      // this is how you can create a contract using new keyword
    28      D d = new D(4);
    29      // this above line will be executed as part of C's constructor
    30  
    31      // you can also create a contract which is already defined inside a function like this
    32      function createD(uint arg) public {
    33          new D(arg);
    34      }
    35  
    36  
    37      // You can also create a contract and at the same time transfer some ethers to it.
    38      function createAndEndowD(uint arg, uint amount) public payable returns (uint){
    39          // Send ether along with the creation of the contract
    40          D newD = (new D).value(amount)(arg);
    41          return newD.getD();
    42      }
    43  }