jdo*_*ner 1 c++ compiler-construction pointers const
可能重复:
为什么我的返回类型无意义?
嗨,我对特定的const转换感到困惑.我有类似的东西
// Returns a pointer that cannot be modified,
// although the value it points to can be modified.
double* const foo()
{
static double bar = 3.14;
return &bar;
}
int main()
{
double* const x = foo(); // fine
const double* y = foo(); // eh?!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在MSVS 2008(Express)上编译它时没有错误,但在我看来应该有.x和y背后的含义是完全不同的,所以似乎不应该有这种隐式转换.这是编译器的问题(不太可能),或者我对这里涉及的常量的理解(很可能).
你所做的相当于以下几点:
const int a = 5;
int b = a;
Run Code Online (Sandbox Code Playgroud)
这是(正如您可能知道的)完全有效,因为它创建了变量的副本.如果你这样做b = 10,a仍然是5,所以常量不会"破坏".