有关合同调用另一个合同的问题 [英] Questions about contract calling another contract
问题描述
需要解决两个与Solidity相关的问题。
Need help with two related Solidity questions.
-
问题1.说,我有一份合同,其中一项要求另一个: / p>
Question 1. Say, I have a contract calling another one:
contract B {
function f1() {
...
}
}
contract A {
B b;
function f() {
b.f1();
}
}
f1
的 msg.sender
与 f()$ c的相同$ c>?它是合同的地址
A
?
Will msg.sender
for f1
be same as for f()
? Of will it be an address of a contract A
?
-
问题2。
说,我有合同A和B。
我想拥有
Question 2. Say, I have contracts A and B. I want to have
contract A {
B b;
A(address addr) {
b = B(addr);
}
}
用另一种语言,我将在声明中使用 B b = null;
以避免双重初始化,但在Solidity中不起作用。那么,如何声明一个成员变量,然后按地址对其进行初始化呢?
In other language, I would use B b = null;
in declaration, to avoid double initialization, but it does not work in Solidity. So how do I declare a member variable and then initialize it by address?
推荐答案
Will msg。 f1的发送者与f()相同?
a合同A的地址是什么?
Will msg.sender for f1 be same as for f()? Of will it be an address of a contract A?
msg.sender
将是合同A的地址。如果要引用原始呼叫者,请使用 tx.origin
。
msg.sender
will be the address of contract A. If you want to reference the original caller, use tx.origin
.
那么我如何声明一个成员变量,然后通过
地址初始化它?
So how do I declare a member variable and then initialize it by address?
声明成员变量时,您不必担心初始化。 Solidity中的所有变量均具有默认值。您可以执行以下操作:
You don't need to worry about initialization when you declare the member variable. All variables in Solidity have default values. You can follow something like this:
contract B {
address sender;
function B(address addr) {
sender = addr;
}
}
contract A {
B b;
function A(){
b = B(msg.sender);
}
}
这篇关于有关合同调用另一个合同的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!