const SimpleClass * const Myfunction()
{
return sc;
}
decltype(auto) p = Myfunction();
p = nullptr; // error due to the second const.
但事实是,没有多少人使用decltype(自动),而你的函数将被正常调用,如:
const SimpleClass *p = Myfunction();
p = nullptr; // success, you are not required to specify the second const.
const auto* p = Myfunction();
p = nullptr; // success, again: we are not required to specify the second const.
而且......
const SimpleClass * const p = Myfunction();
p = nullptr; // error
const auto* const p = Myfunction();
p = nullptr; // error
来源
2016-03-05 17:00:09
Jts
http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int -const –
It * does *返回一个常量指针给一个常量对象。您可以将该指针的*值*复制到非const变量。 (它与const int f(){return 0;}的原理相同。int main(){int x = f(); x = 1;}'。) – molbdnilo
想要另一个* const *?将它添加到MyFunction()行的末尾。 ;) – tofro