Global variable
-
Dear all, How can I declare a variable so that all cpps in my MFC project can access the variable? Thanks. Vickie
in whatever.h: extern int myGlobalVariable; in whatever.cpp int myGlobalVariable = 0; in any file you want to use myGlobalVariable: #include "whatever.h" -c ------------------------------ Smaller Animals Software, Inc. http://www.smalleranimals.com
-
Dear all, How can I declare a variable so that all cpps in my MFC project can access the variable? Thanks. Vickie
Like this (applies to C++ not just MFC): yourcode.h: extern globalVariableType YourGlobalVariable; yourcode.cpp: globalVariableType YourGlobalVariable; This is freqently use for making the MFC CWinApp class global like this: extern CMyApp theApp; (this is because theApp is already defined in your MyApp.cpp file by MFC). This nicely replaces: ((CMyApp*)AfxGetApp())->Bogus(); with: theApp.Bogus(); Hope this helps -------------------------------- Todd C. Wilson (tcw@nopcode.com) www.nopcode.com ICQ: 5638028 "I picked up a Magic 8-Ball the other day and it said 'Outlook not so good.' I said, 'Sure, but Microsoft still ships it.'"
-
in whatever.h: extern int myGlobalVariable; in whatever.cpp int myGlobalVariable = 0; in any file you want to use myGlobalVariable: #include "whatever.h" -c ------------------------------ Smaller Animals Software, Inc. http://www.smalleranimals.com
-
I can't get it work even I have followed what you'd said, since there are many others header files included in my cpps. Will this be the reason leading to my errors? Thanks a lot!
Be sure to include the header with the 'extern' wherever you are going to use it. Or stick the header include in your stdafx.h