通过 MSDN 挖掘,我遇到了另一条奇怪的线路:
// This function returns the constant string "fourth".
const string fourth() { return string("fourth"); }
完整的示例埋在这里:https : //msdn.microsoft.com/en-us/library/dd293668.aspx 精炼到最低限度,它看起来像这样:
#include <iostream>
const int f() { return 0; }
int main() {
std::cout << f() << std::endl;
return 0;
}
其他一些具有不同返回类型的测试表明,Visual Studio 和 g++ 都在没有警告的情况下编译这样的行,但 const 限定符似乎对我可以对结果执行的操作没有影响。任何人都可以提供一个重要的例子吗?