C++ 如何访问在另一个函数中的一个函数中定义和声明的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11783435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access variables defined and declared in one function in another function?
提问by Varun Chitre
Can anyone tell me how to access variables declared and defined in a function in another function. E.g
谁能告诉我如何访问在另一个函数的函数中声明和定义的变量。例如
void function1()
{
string abc;
}
void function2()
{
I want to access abc here.
}
How to do that? I know using parameters we can do that but is there any other way ?
怎么做?我知道使用参数我们可以做到这一点,但还有其他方法吗?
采纳答案by JoeFish
The C++ way is to pass abc
by reference to your function:
C++ 方式是abc
通过引用传递给您的函数:
void function1()
{
std::string abc;
function2(abc);
}
void function2(std::string &passed)
{
passed = "new string";
}
You may also pass your string as a pointer and dereference it in function2. This is more the C-style way of doing things and is not as safe (e.g. a NULL pointer could be passed in, and without good error checking it will cause undefined behavior or crashes.
您也可以将字符串作为指针传递并在 function2 中取消引用它。这更像是 C 风格的做事方式,并不安全(例如,可以传入 NULL 指针,如果没有良好的错误检查,它将导致未定义的行为或崩溃。
void function1()
{
std::string abc;
function2(&abc);
}
void function2(std::string *passed)
{
*passed = "new string";
}
回答by BenN
Make it global then both can manipulate it.
将其设为全局,然后两者都可以操作它。
string abc;
void function1(){
abc = "blah";
}
void function2(){
abc = "hello";
}
回答by StarPilot
If you have a variable in function1 that you want to use in function2, then you must either:
如果您想在 function2 中使用 function1 中的一个变量,那么您必须:
- pass it directly,
- have a higher scope function that calls both declare the variable and pass it, or
- declare it a global and then all functions can access it
- 直接传过去,
- 有一个更高范围的函数,它调用声明变量并传递它,或者
- 将其声明为全局,然后所有函数都可以访问它
If your function2 is called from function1, then you can just pass it as an argument to function2.
如果您的 function2 是从 function1 调用的,那么您可以将它作为参数传递给 function2。
void function1()
{
std::string abc;
function2( abc );
}
void function2( std::string &passed )
{
// function1::abc is now aliased as passed and available for general usage.
cout << passed << " is from function1.";
}
If function1 doesn't call function2, but both are called by function3, then have function3 declare the variable and pass it to both function1 and function2 as an argument.
如果 function1 不调用 function2,但两者都被 function3 调用,则让 function3 声明变量并将其作为参数传递给 function1 和 function2。
void parentFunction( )
{
std::string abc;
function1( abc );
function2( abc );
}
void function1( std::string &passed )
{
// Parent function's variable abc is now aliased as passed and available for general usage.
cout << passed << " is from parent function.";
}
void function2( std::string &passed )
{
// Parent function's variable abc is now aliased as passed and available for general usage.
cout << passed << " is from parent function.";
}
Finally, if neither function1 nor function2 is called from each other, nor from the same function in code, then declare the variable to be shared as a global, and function1 and function2 will be able to directly use it.
最后,如果 function1 和 function2 没有被彼此调用,也没有从代码中的同一个函数中调用,那么将要共享的变量声明为全局变量,并且 function1 和 function2 将可以直接使用它。
std::string global_abc;
void function1( )
{
cout << global_abc << " is available everywhere.";
}
void function2( )
{
cout << global_abc << " is available everywhere.";
}
回答by Kirill Kobelev
There is absolutely no way. Variables of the block can be directly accessed ONLY from that block.
绝对没有办法。块的变量只能从该块直接访问。
Pointers/references to the object can be passed into functions that are called from this block as parameters.
对象的指针/引用可以作为参数传递到从此块调用的函数中。