如何在多个函数中使用一个结构体
我正在使用一个结构,
struct IF_ID {
int PC;
string instruction;
};
然后在
int main()
{
IF_ID stage1;
stage1.PC=0;
FETCH(stage1);
DECODE(&stage1);
return 0;
}
当我在 FETCH(stage1)
函数中传递 stage1
时它工作正常,问题是我需要具有的值在 FETCH(stage1)
中计算,将在第二个函数 DECODE(stage1)
中再次使用 例如,如果 stage1.PC
等于 5,我需要在 DECODE(stage1)
函数中重用它,我该怎么做?
I am using a structure such as
struct IF_ID {
int PC;
string instruction;
};
and then in the
int main()
{
IF_ID stage1;
stage1.PC=0;
FETCH(stage1);
DECODE(&stage1);
return 0;
}
When I passe stage1
in the FETCH(stage1)
function it works fine, the thing is that i need the values that has been calculated in the FETCH(stage1)
to be used again in the second function DECODE(stage1)
so if the stage1.PC
is equal to 5 for example i need to reuse it in the DECODE(stage1)
function how can i do that??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
评论(5)
您正在按值将结构传递给 FETCH()。
这意味着它被复制到函数中。
如果你想传递实际的结构,你需要方法来接收它的指针或引用:
通过引用:
指向结构的指针:
如果你通过引用使用,内部函数语义不会改变。
You're passing the struct to FETCH() by value.
This means it is COPIED to the function.
If you want the actual struct to be passed, you need the method to receive its pointer or reference:
By reference:
pointer to struct:
If you use by reference, the inner function semantics wont change.
如果您希望
FETCH
更改参数值,它应该接受指向您的结构的引用或指针作为参数。 或者它可以返回一个实例IF_ID
。If you want
FETCH
to change the value of parameter, it should accept either reference or pointer to your structure as parameter. Or it can return an instanceIF_ID
.解决方案是将
stage1
作为对FETCH()
函数的引用传递。 函数签名应该看起来像这样:或像这样:
在后一种情况下,函数调用还需要在参数前加上一个符号:
The solution is to pass
stage1
as a reference to theFETCH()
function. The function signature should look either like this:or like this:
in the latter case, the function call also needs an ampersand sign in front of the parameter:
不完全确定你想做什么,但让我猜猜。
您有一个类型和两个函数(FETCH 和 DECODE),它们在该类型的输入参数上运行。 第一个函数修改输入参数,您希望将修改后的值用作第二个函数的输入。
也许您应该将函数声明为(让我们称您的类型为 T,因为这可能适用于任何复杂类型或类)。
这样类型 T 的参数 x 通过引用传递。 在函数体内执行的修改将影响函数外部的结果。
然后您可以将第二个函数声明为:
这里您再次通过引用传递复杂类型,但这次您保证您不会修改它 (const)。
Not completely sure what you're trying to do, but let me guess.
You have a type and two functions (FETCH and DECODE) that operate on an input parameter of that type. The first function modifies the input parameter and you want the modified values to be used as input to the second function.
Probably you should declare the functions as (let's call your type T, since this probably apply to any complex type or class).
This way the parameter x of type T is passed by reference. The modifications performed within the body of the function will affect the result outside of the function.
Then you can declare the second function as:
Here you're again passing the complex type by reference, but this time you promise that you're not going to modify it (const).
Yochai Timmer 和其他几个人的答案是正确的,但也考虑将你的结构变成一个类,并将这些函数作为类的成员,因为它们主要是在它上面运行。 然后代码看起来像这样:
但是,如果 FETCH 除了 PC 之外不使用 IF_ID 的任何值,并且根据代码的组织方式,让 FETCH 构造对象而不是使用“空白”对象可能会更清楚传入。例如,类似于:
struct IF_ID {
国际个人计算机;
字符串指令;
IF_ID() : PC(0) {} // 确保默认有 PC=0
};
IF_ID 获取(){
IF_ID 阶段 1;
// 获取东西并将其放入 stage1
返回阶段1; // 如果 IF_ID 是一个复制成本很高的大类,
// 确保你的编译器支持“named return
// 价值优化”,即不产生
// 成员变量之间的多余拷贝
// 和返回值。
}
int main() {
IF_ID 阶段 1 = 获取 ();
解码(第一阶段);
}
Yochai Timmer and several others' answers are correct, but also consider making your struct into a class, and making these functions members of the class, since they are primarily operating on it. Then the code would look something like:
However, if FETCH uses none of the values of IF_ID except PC, and depending how your code is organised, it may be even clearer to make FETCH construct the object, rather than having a "blank" object passed in. For instance, something like:
struct IF_ID {
int PC;
string instruction;
IF_ID() : PC(0) {} // Ensure default has PC=0
};
IF_ID Fetch() {
IF_ID stage1;
// do fetch stuff and put it in stage1
return stage1; // If IF_ID is a large class expensive to copy,
// make sure your compiler supports "named return
// value optimisation", ie. doesn't produce a
// superfluous copy between the member variable
// and the return value.
}
int main() {
IF_ID stage1 = Fetch();
Decode(stage1);
}