为什么这样:
#include <string>
#include <iostream>
using namespace std;
class Sandbox
{
public:
Sandbox(const string& n) : member(n) {}
const string& member;
};
int main()
{
Sandbox sandbox(string("four"));
cout << "The answer is: " << sandbox.member << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
答案是:
代替:
答案是:四
以下引用来自Addison Wesley的C++模板.有人可以帮助我用简单的英语/外行人的术语来理解它的要点吗?
因为字符串文字是具有内部链接的对象(两个字符串文字具有相同的值但在不同的模块中是不同的对象),所以您不能将它们用作模板参数: