When to use extern in C++

前端 未结 4 2125
清歌不尽
清歌不尽 2020-11-22 03:15

I\'m reading \"Think in C++\" and it just introduced the extern declaration. For example:

extern int x;
extern float y;

I thi

4条回答
  •  执念已碎
    2020-11-22 03:49

    It is useful when you share a variable between a few modules. You define it in one module, and use extern in the others.

    For example:

    in file1.cpp:

    int global_int = 1;
    

    in file2.cpp:

    extern int global_int;
    //in some function
    cout << "global_int = " << global_int;
    

提交回复
热议问题