c++全局变量,多模块使用

来源:互联网 发布:淘宝客服业绩 编辑:程序博客网 时间:2024/06/05 12:26

用extern修饰的全局变量
    在test1.h中有下列声明:

    #ifndef TEST1H    #define TEST1H    extern char g_str[]; // 声明全局变量g_str    void fun1();    #endif


    在test1.cpp中

    #include "test1.h"        char g_str[] = "123456"; // 定义全局变量g_str        void fun1()    {        cout << g_str << endl;    }


    以上是test1模块, 它的编译和连接都可以通过,如果我们还有test2模块也想使用g_str,只需要在原文件中引用就可以了

    #include "test1.h"    void fun2()    {        cout << g_str << endl;    }


 

 

http://hi.baidu.com/bwandmff/item/a1e35de56d019c276dabb8d7

原创粉丝点击