Ethereum Asked by zak100 on January 2, 2021
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.
Based on the clarification in the comments to smarx's answer.
It depends what you mean by "problem". Consider this function.
function changeOwner() public {
owner = msg.sender;
}
That allows anyone to seize the owner role, assuming owner
is pivotal to access control somewhere else. That's not a problem in the sense that it works. That is a problem in the sense that anyone can do it and that's usually not desirable.
Consider this variant:
function changeOwner(address newOwner) public {
require(msg.sender = owner);
owner = newOwner;
}
The require()
strictly limits this to the owner
, so the sender has to be the owner in order to relinquish ownership. It doesn't make sense to relinquish to itself, so another address (the newOwner
) is passed in. A production-ready implementation of this common pattern would check that the new address is valid to prevent accidents.
It may be helpful to remember that owner
is not special in any way. It's merely a variable of type address
given meaning by other aspects of the contract such as conditionals that oversee permission.
Hope it helps.
Answered by Rob Hitchens on January 2, 2021
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 aprivate state variable
like thisaddress private owner = msg.sender;
then solidity compiler treat owner variable asvariable
and for getting this variable value you need to define another function likegetOwnerAddress()
.Public:
If you set
owner
as public like thisaddress public owner = msg.sender;
then solidity compiler treat owner variable asfunctionowner()
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.
Answered by Mahesh Rajput on January 2, 2021
Statements aren't allowed outside of functions, but initializations are.
So your second snippet (address private owner = msg.sender;
) is good.
Answered by user19510 on January 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP