Saturday, 14 September 2013

Why is this code compiled and linked without an error?

Why is this code compiled and linked without an error?

Isn't the extern keyword supposed to simply 'blind' the compiler? Here are
the codes that i can't understand why it is without an error.
struct A {
int a;
};
class B {
static A x;
public:
void f() { x.a=0 }
};
extern A B::x; // not allocated.
main() {
B z;
z.f();
}
As you already know, the static member should be instantiated manually.
However, i added the extern keyword, which means it's not actually
allocated. It is very weird that it compiles fine!

No comments:

Post a Comment