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