current position:Home>Can this be used in the smart contract constructor?

Can this be used in the smart contract constructor?

2022-02-03 20:46:56 Q & A of Denglian community

When is the address of the smart contract valid ? You can use this Do you ?

Take the answer 1:

The usual intuition is that smart contracts should be constructed after ( Constructor constructor After execution ), It's not , stay constructor You can use this To indicate the address of the current contract , Because when you start a deal , You can already determine the address of the contract , Reference resources Calculation of contract address .

You can use the following code to test :

pragma solidity ^0.5.0;contract Test {  address public thisAddress;  event LogAddr(address);  constructor()  {    thisAddress = address(this);    emit LogAddr(address(this));  }}

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.

Random recommended