1

Hi can anyone tell me the differnece between creating contract using a new operator and creating contract by passing the address.

Example:

contract A{
    uint256 public num = 10;
    function get() returns(uint){
        return num;
    }
}

contract B{
    function get() returns(uint){
        A a = new A();
        a.get();
    }
}

and

contract B{
    function get(address add) returns(uint){
        A a = A(add);
        a.get();
    }
}
2

If you want to deploy a contract with new address that was not exist in network before then we can use

A a = new A();

It generate address a of the contract A . Here a holds the address of contract which was just deployed.

If you have address of contract which was already deployed in the network and you want to use in current contract then you have to convert that address to contract type. After this It will be recognized as this particular address is of contract address type.

A a = A(address_of_contract_A);

This will avoid the confusion of either address is EOA(externally owned account) or CA(contract account).

0

In the first scenario, each time you call B.get(), you will deploy a new instance of A. If you call it 10 times, there will be 1 B contract, and 10 A contracts.

In the second scenario, contract A is already deployed somewhere, and you simply pass its address to the get call. In this scenario, a single instance of A can be used multiple times.

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.