简体   繁体   中英

Operator from Ethereum contract _;

Sorry if it is duplicated question. What does "_" operator make?

This code is from Ethereum contract manual: https://ethereum.org/token#deploying

contract owned {
    address public owner;

    function owned() {
        owner = msg.sender;
    }

    modifier onlyOwner {
        if (msg.sender != owner) throw;
        _;
    }

    function transferOwnership(address newOwner) onlyOwner {
        owner = newOwner;
    }
}

It is used inside modifiers.

"The function body is inserted where the special symbol "_" in the definition of a modifier appears."

Reference: Contracts — Solidity 0.4.19 documentation

And in case it is used only once in a modifier, you can see it as following: After the return variables are assigned, the _ returns the control flow to what is next to the current modifier (next to the current modifier could be the next modifier or the function).

You can see a detailed explanation and example in the answer at: Are underscores _ in modifiers code or are they just meant to look cool?

Modifiers are used to enforce pre/post-conditions on the execution of a function.

The _ operator is a shorthand that represents the actual code of the function that you are "modifying" .

So in your code, every time that transferOwnership is invoked, the onlyOwner modifyer will get into play first.

  • If you are not the owner, the execution will just throw , roll everything back and exit.
  • If you are the owner, the control flow will reach the _ operator, so the transferOwnership statements will be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM