【发布时间】:2023-03-08 03:17:02
【问题描述】:
这个问题与随后提出的问题here.密切相关
Stroustrup 描述了定义类内常量的方法here。
当我遵循 Stroustrup 的方法时,我看到了预期的结果。但是,在 Visual Studio 2010 中,调试器无法解析该类范围内的 static const 类成员。
这就是我的意思:
#include <iostream>
class Foo {
public:
static const int A = 50;
char arr[A];
void showA();
};
void Foo::showA() {
std::cout << "showA = " << A << "\n";
}
int main() {
Foo f;
f.showA();
}
当调试器在 showA() 中时,“watch”窗口会报告:
Error: Symbol "Foo::A" not found
我想强调一下,该程序确实按预期运行,即输出是:
showA = 50
程序返回 0。
其他人可以用 Visual Studio 2010 重现这个吗?这是调试器中的错误吗?
【问题讨论】:
-
请注意,您缺少
A静态数据成员的定义 -
它是内联的,对 OP 而言
-
是否启用优化?类是否在命名空间内?
-
@Andy 如果它是内联的,则不是。正如 Stroustrup 文章和 dauphic 的回答所指出的,静态定义是可选的。我刚刚在 VS 中测试了两种方式,并且内联初始化不需要静态 def'n。
-
@AndyProwl 再次感谢 - 所以这里最好
标签: c++ visual-studio gcc compiler-construction debuggervisualizer