0

I am trying to understand the syntax of C c = new C();

I read the new keyword deploys, initializes state variables, runs the constructor, sets nonce to one, and returns address of new instance.

I read that a state variable a has a 0-ary public getter function a() that returns the value of a.

Questions:

  1. What "is" the getter function of a contract?
  2. Why does new act on the getter function?
  3. In C c = new C(); why do we need the first C to define the variable?

1 Answer 1

1

A contract doesn't have a "getter" function. You could read C() as calling the contract's constructor (but the call isn't like regular functions).

The part new C() is the solidity syntax to deploy the C contract. The constructor accepts parameters so they can be passed like this new D(param1, param2, param3).

The requirement to declare the type returned in the assignment C c = ... is for security reasons. A few versions back solidity supported var c = ... but there were a few contracts with subtle bugs due to the wrong type deduced by the compiler.

1
  • Asked a follow-up question here
    – user516728
    Mar 14, 2022 at 0:34

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.