关灯

Using "owner = msg.sender" in a non-constructor function or in a decla

  [复制链接]
区块链技术 发表于 2019-1-2 21:27:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
 

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x

Will it be a problem to use :

 owner = msg.sender;

statement in a non-constructor function?

What if instead of writing the above code in a function, we initialize the owner at the time of declaration like this in a state variable:

address private owner = msg.sender;

Some body please guide me.

Zulfi.




上一篇:TypeError: Member "adopt" not found or not visible after argument-depe
下一篇:币圈明辰:3月28日 BTC EOS ETH 参考策略操作建议
版权申明:此文如未标注转载均为本站原创,自由转载请注明 本贴地址:https://xuesq.cn/thread-109616-1-1.html 上篇帖子: Bit-Z将于1月4日16:00上线EDR 下篇帖子: 星月解惑:如何做好投资?而不是被市场玩弄
回复

使用道具 举报

 

精彩评论2

正序浏览
区块链技术 发表于 2019-1-2 21:27:52 | 显示全部楼层
 

Statements aren't allowed outside of functions, but initializations are.

So your second snippet (address private owner = msg.sender;) is good.

回复

使用道具 举报

 
 楼主| 区块链技术 发表于 2019-1-2 21:31:56 | 显示全部楼层
 

owner = msg.sender; statement in a non-constructor function?

If you write owner = msg.sender; in a non-constructor function then 1) You have to pay more gas amount(approx 1.5x more) as compared to we deploy this statement using the constructor.

2) You need to call a function to set owner = msg.sender when you are using the non-constructor function.

What if instead of writing the above code in a function, we initialize the owner at the time of declaration like this in a state variable:

There is no problem with initializing owner at the time of declaration but it changes the meaning of state variable for solidity compiler. let me explain you:

Private:

When you declare owner as a private state variable like this address private owner = msg.sender; then solidity compiler treat owner variable as variable and for getting this variable value you need to define another function like getOwnerAddress().

Public:

If you set owner as public like this address public owner = msg.sender; then solidity compiler treat owner variable as functionowner() and this will also return the address value of owner there is no need to define a separate function for getting value.

For more understanding, you can see a difference in abi of contract details.

回复

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

排行榜

关注我们:微信订阅号

官方微信

APP下载

全国服务邮箱:

admin@xuesq.cn

公司地址:重庆南岸区学府大道

运营中心:重庆微生派网络工作室

邮编:610066 Email:1401171837#qq.com

Copyright   ©2017-2018  中比特Powered by©xuesq.cn!技术支持:中比特    ( 蜀ICP备16009247号-1 )