Thread: Question about "Extern const"

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Question Question about "Extern const"

    I define:

    Code:
    extern const int i2 = 110;
    in C++, compiler reports an error as:

    error C2205: 'i2' : cannot initialize extern variables with block

    What does it mean?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    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:

    Code:
    // xx1.cpp
    const int I3 = 110;
    
    
    // xx2.cpp
    const int I4 = 220;
    What's the point of using "extern const"?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM