2

Is deploying a contract analogous to instantiating a plaint old class in OOP and the address returned similar to the memory pointer of the OOP object? Should I be thinking of the ledge as memory to a running program?

If yes, then if I’m looking to do have something like an array of objects then I would have another contract that creates a new contract and store it’s address in array field?

Zeid S.
  • 21
  • 1

1 Answers1

1

Actually deploying a contract is not analogous to instantiating an object.

Deploying a contract is like publishing your program for the world to see/use. So when you deploy your contract you create a version of it and publish it. The difference is that regular programs are typically available at the same "address" with a new version but deploying the same contract (or its updated version) never uses the same address.

However you can instantiate a new contract from within an existing contract by doing something like MyContract contr = new MyContract() and this creates the contract and its address (and functionality) is accessible from the contr variable. Or you can simply create a reference to an existing contract with MyContract contr = MyContract(address_of_contract). In both cases the contr variable is an instance of the contract and is therefore analogous to the OOP world's instantiation.

So, yes, if you create contract within a contract you can store those addresses within an array.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
  • Analogies are always imperfect, but I would say that yes, deploying a contract is analogous to instantiating an object. – user19510 Jun 29 '19 at 21:38
  • Thank you for the response Lauri. Perhaps this depends on some items (allow me to expand). If I use remix and I want to create a modifier that checks on owner, then setting the owner is done in the constructor. And the since the constructor is called once, I had assumed that this was done when the contract was first deployed. I suppose using other than remix, deploying a contract would be done in a different way and then calling the constructor passing the address of the account owner would be the way the contract is instantiated. – Zeid S. Jun 30 '19 at 05:26
  • Your opening statement and the explanation which follows it are confusing. The explanation does not imply that "deploying a contract is not analogous to instantiating an object", it implies that "deploying a contract is not analogous to publishing a regular program". – goodvibration Jun 30 '19 at 06:06
  • Well, there is obviously no right answer to this. I expressed my opinion, you are free to add another answer with your own opinions. I also disagree with you @goodvibration about the irregularity - maybe we just interpret the text differently. Thanks for the comments nevertheless :) – Lauri Peltonen Jun 30 '19 at 08:30
  • I did not suggest that your answer was wrong in any way. I just said that the explanation to the opening statement doesn't imply this statement, but something else. – goodvibration Jun 30 '19 at 08:39