I define:
in C++, compiler reports an error as:Code:extern const int i2 = 110;
error C2205: 'i2' : cannot initialize extern variables with block
What does it mean?
Printable View
I define:
in C++, compiler reports an error as:Code:extern const int i2 = 110;
error C2205: 'i2' : cannot initialize extern variables with block
What does it mean?
Usually you initialize something where you define it. If it is extern, you are saying it is defined elsewhere, so it makes little sense to say it is(will be) elsewhere (defined as) whatever.
Can I do this:
Code:// xx.h
extern const int I2;
// xx1.cpp
#include "xx.h"
const int I2 = 110;
// xx2.cpp
#include "xx.h"
const int I2 = 220;
What will happen? Are there conflictions?
If not, seems that "extern const" doesn't make any sense, because you can always define:
What's the point of using "extern const"?Code:// xx1.cpp
const int I3 = 110;
// xx2.cpp
const int I4 = 220;
> What will happen? Are there conflictions?
Did you try it?
> What's the point of using "extern const"?
Same as any other use of const, to stop you from changing things which should not be changed.