3

对不起,如果它是重复的问题。“_”运算符是做什么的?

此代码来自以太坊合约手册: 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;
    }
}
4

2 回答 2

0

它用于修饰符内部。

“函数体插入到修饰符定义中出现特殊符号“_”的位置。”

参考: 合约 — Solidity 0.4.19 文档

如果它在修饰符中只使用一次,您可以看到它如下:返回变量分配后,_将控制流返回到当前修饰符旁边的内容(当前修饰符旁边可能是下一个修饰符或函数)。

您可以在答案中看到详细的解释和示例: 修饰符代码中的下划线_还是它们只是为了看起来很酷?

于 2017-11-17T20:42:37.843 回答
0

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.
于 2018-03-27T23:38:59.480 回答