有人可以解释C 操作员=此处的工作方式
Can someone explain how the c++ operator= here works?
我有这个小的C 代码段,有人可以解释操作员如何在这里工作吗?
#include <iostream>
#include <string>
using namespace std;
static wstring & test() {
static wstring test2;
return test2;
};
int main()
{
test() = L"Then!";
wcerr << test() << endl;
}
函数test()
正在将引用(不是副本(返回到静态变量test2
。静态关键字使该函数test
保持呼叫之间可变test2
的值。因此,当您调用test()
时,它会返回参考,允许您更改test()
内test2
的值。这导致wcerr << test2 << endl;
打印出"然后!"打印出来。
请注意,静态关键字的含义取决于上下文。使该函数静态使该函数仅可在文件中的其他函数中可见。如果将静态功能放在标题中,则该功能的每个#include都会减速。
您可能想说的是
#include <iostream>
#include <string>
using namespace std;
wstring & test() {
static wstring test2;
return test2;
}
int main()
{
test() = L"Then!";
wcerr << test() << endl;
}
函数 test()
将a 参考返回到static
变量test2
。引用是指变量;您可以代替参考。
这等同于代码:
static wstring test2;
int main()
{
test2 = L"Then!";
wcerr << test2 << endl;
}
搜索您喜欢的C 引用"参考"。
相关文章:
- 在 Eclipse 中添加库的工作方式是否与在 Visual Studio 中相同?
- OpenGL应用程序在不同的计算机上的工作方式不同
- 为什么我的代码在添加不相关的代码行后工作方式不同?
- wait_until在主线程而不是主线程中的工作方式不同吗? c ++
- 用于了解输入和输出流缓冲区实际工作方式的程序
- 查找函数在unordered_map中的工作方式是搜索键值
- 尝试使用 Qt 库中的 QPixmap 将图像拆分为多个块。关于他的复制方法的工作方式,我有什么不明白的吗?
- 容器类别在STL中的工作方式
- Unity 的 HLSL/Cg 预处理器工作方式错误?
- 无法让我了解 cin.get 和 cout 在这里的工作方式
- 不了解C 集合的工作方式
- 有人可以解释C 操作员=此处的工作方式
- 了解 declval 在copy_assignment情况下的工作方式
- 打开文件的正确模式是什么,以便 seekp() 的工作方式与在默认模式下打开的文件相同
- 了解文件页在技术级别的工作方式
- 战俘的工作方式不同,详细解释
- 切换到新编译器后,SSCANF 的工作方式有所不同
- 为什么三元运算符在编译时的工作方式与运行时不同?
- 对运算符删除覆盖的工作方式感到困惑
- 在两种情况下,铸造的工作方式不同