【发布时间】:2010-08-22 00:32:43
【问题描述】:
您好,我对特定的 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;
}
当我在 MSVS 2008 (Express) 上编译它时没有错误,但在我看来应该有。 x 和 y 背后的含义大相径庭,所以似乎不应该有这种隐式转换。那么这是编译器的问题(不太可能),还是我对这里涉及的 const-ness 的理解(很可能)。
【问题讨论】:
-
请不要使用
<code>标签来格式化您的代码。使用提供的代码格式化按钮。
标签: c++ pointers compiler-construction constants