【发布时间】:2014-06-19 17:41:13
【问题描述】:
this C++ FAQ 试图传达什么?
当(且仅当)静态成员具有类外定义时,您可以获取静态成员的地址:
class AE {
// ...
public:
static const int c6 = 7;
static const int c7 = 31;
};
const int AE::c7; // definition
int f()
{
const int* p1 = &AE::c6; // error: c6 not an lvalue
const int* p2 = &AE::c7; // ok
// ...
}
不过这个compiles !
【问题讨论】:
-
如果分别编译
class AE和f()然后链接对象会怎样? -
@juanchopanza 它与
-O2和f()编译本质上是一个nop。但是,它使用-O0会导致链接器错误,就像原始程序使用-O0一样。 -
@Csq 是的,我看到并赞成您的回答。或者,
std::cout << p1 << std::endl;将在操作中进行。 -
这是一个相关链接,最初在我对已删除答案的评论中:en.wikipedia.org/wiki/…
标签: c++