能在所有类中访问的全局变量

解决方案 »

  1.   

    在任意一个Cpp文件中声明就行啊,
    如://in ex1.cpp
    int a;        //全局变量定义然后在其它的需要这个变量的Cpp文件中写
    //in ex2.cpp
    extern int a;  //外部变量声明就可以了。
      

  2.   

    在ex2.cpp文件中应用ex1的头文件
      

  3.   

    你写在某个CPP中,直接用就可.别的CPP用到时候用EXTERN.最好符合命名规则.
      

  4.   

    C***App中定义一个公有的CString
    使用时
    void CDlgDlg::OnButton2() 
    {
    ((CDlgApp*)AfxGetApp())->str = "test";
    }
    不要忘记了包含头文件
      

  5.   

    我在公司都是用的zeroxy(水蓝) 技术
      

  6.   

    我也用zeroxy(水蓝) 的那种方法
      

  7.   

    在文件范围内声明变量,其他文件中用extern声明
      

  8.   

    在 A.h 中
     extern int m;
     在 A.cpp 中,
     int m;然后要用 m 时
    #include "A.h"
      

  9.   

    下午我一直想回复,一直发不了贴!这下被大家都说忘了,55555555反正切记不要试图在.h里面定义一个非static的全局变量,那样会报编译出错的!典型的,就如楼上所说的!
    在 A.h 中
     extern int m;
     在 A.cpp 中,
     int m;然后要用 m 时
    #include "A.h"
      

  10.   

    同意楼上的,顺便问一下,能不能
    在 A.h 中
    int m;
    在 A.cpp 中,
    extern int m;
    还有Static和extern能不能同时存在?
      

  11.   

    请问一下,我在w2view.app中定义了Bool a=false;在w2view.h中声明extern bool a;
    然后我在对话框中用a,在dlg.app中包含了w2view.h  运行却出现以下错误,不知道什么原因,请高手指教?
    d:\日历1\w2view.h(21) : error C2143: syntax error : missing ';' before '*'
    d:\日历1\w2view.h(21) : error C2501: 'CW2Doc' : missing storage-class or type specifiers
    d:\日历1\w2view.h(21) : error C2501: 'GetDocument' : missing storage-class or type specifiers
    Error executing cl.exe.
    Creating browse info file...
      

  12.   

    在任意一个Cpp文件中声明就行啊,
    如://in ex1.cpp
    int a;        //全局变量定义然后在其它的需要这个变量的Cpp文件中写
    //in ex2.cpp
    extern int a;  //外部变量声明就可以了。
      

  13.   

    可以随便在那个cpp中声明一个变量,在所有函数体的外面,然后在使用这个变量的地方用
    extern "变量"
      

  14.   

    在以在任何一个CPP文件中声明!
    如下:
    //*.cpp
    int a;int *()//函数实现
    {
    }
    如果要在其它文件中使用
    使用extern再声明一个就可以了
    如下:
    extern int a;
      

  15.   

    谢谢大家啦,特别是: zeroxy(水蓝) ^^!