智能合约构造函数中可以使用this吗?

Tiny熊 发布于 2019-07-25 阅读 14598

智能合约的地址什么时候有效? 在构造函数中可以使用this吗?

智能合约的地址什么时候有效? 在构造函数中可以使用this吗?

通常的直觉是智能合约应该在构造完之后(即构造函数constructor执行完之后),其实并不是,在constructor中是可以使用this来表示当前合约的地址,因为在发起交易的时候,就已经可以确定合约的地址,参考合约地址的计算

可以使用下面的代码测试一下:


pragma solidity ^0.5.0;

contract Test {
  address public thisAddress;
  event LogAddr(address);

  constructor()  {
    thisAddress = address(this);
    emit LogAddr(address(this));
  }
}

学习中如遇问题,欢迎到区块链技术问答提问,这里有老师为你解惑。

相关文章

0 条评论