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?
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.