在C++中爲類定義運算符賦值時,我注意到您不需要返回* this。我在Visual Studio 2012,Visual Studio 2010中測試了這個警告級別爲4,並且沒有錯誤或警告,並且代碼按預期工作。但在Visual Studio 2008中,我收到了一個編譯時錯誤,說'操作符=必須返回一個值'。C++賦值運算符默認返回
我的問題是:是這樣的「功能」除了核心的C++語言,或者是它的東西是,森林狼隊決定在添加
,如果這已問我道歉或有信息。這在線上,我無法找到任何答案。
template <typename T>
struct singleton
{
T value;
singleton(const singleton& x) : value(x.value) {}
singleton() {}
~singleton() {}
singleton& operator=(const singleton& x) { value = x.value; }
};
這是我用於測試的代碼:
singleton<float> sf;
sf.value = 1.9f;
singleton<float> sf2 = sf;
std::cout << sf2.value << std::endl;
您是否有示例? – 0x499602D2
你有'singleton&'作爲返回值,但不返回任何內容。 – Rapptz
@Rapptz我知道,但它似乎是合法的VS2012即使有警告級別4 – user2574802