C++ 指针(*)和引用(&;)之间的差异
我在这里使用了引用(&),但是a没有改变。为什么? 使用指针(*)时,值已更改C++ 指针(*)和引用(&;)之间的差异,c++,C++,我在这里使用了引用(&),但是a没有改变。为什么? 使用指针(*)时,值已更改 int all=0; void call_from_thread(int & a) { a = 5; } int main() { thread s = thread(call_from_thread, all); s.join(); return 0; } 另一个程序,在本例中,我也使用了Reference(&),但值已更改。为什么线程中的值没有更改 void Fu
int all=0;
void call_from_thread(int & a)
{
a = 5;
}
int main()
{
thread s = thread(call_from_thread, all);
s.join();
return 0;
}
另一个程序,在本例中,我也使用了Reference(&),但值已更改。为什么线程中的值没有更改
void Func3(int &x)
{
x = x + 10;
}
int main() {
int n = 0;
Func3(n);
cout << “n = ” << n << endl; // n = 10
}
void Func3(int&x)
{
x=x+10;
}
int main(){
int n=0;
功能3(n);
cout在线程创建步骤中,std::thread
构造函数复制其参数。……普通引用不会在trip中生存,而call\u from\u thread
函数接收对副本的引用
可以在标准中找到详细信息。thread::thread
构造函数行为描述为
构造thread类型的对象。新的执行线程执行调用(detacy\u COPY(std::forward(f))、detacy\u COPY(std::forward(args))…)
,并调用
正在构造线程中计算的decation\u COPY
decation\u COPY
的确切定义相当复杂,但顾名思义,它忽略了引用并创建了值副本
一个简单的解决方法是使用std::ref
thread s = thread(call_from_thread, std::ref(all));
这会在副本中保留一个指针,并且只有在引用参数绑定时才会解析到实际目标。在线程创建步骤中,std::thread
构造函数会复制其参数。……普通引用不会在trip中存活,而call\u from\u thread
函数会收到一个引用o副本
可以在标准中找到详细信息。thread::thread
构造函数行为描述为
构造thread类型的对象。新的执行线程执行调用(detacy\u COPY(std::forward(f))、detacy\u COPY(std::forward(args))…)
,并调用
正在构造线程中计算的decation\u COPY
decation\u COPY
的确切定义相当复杂,但顾名思义,它忽略了引用并创建了值副本
一个简单的解决方法是使用std::ref
thread s = thread(call_from_thread, std::ref(all));
这会在副本中保留一个指针,并且只有在引用参数绑定时才会解析到实际目标。3)-是的,这很烦人。3)-是的,这很烦人。