-
How can I declare a public variable in C++ (without MFC) and so that I can share this variable in two different cpp file.
example:
I have 2 variable name ptX and ptY both are double data type.
I need to store a value at runtime into this variable at a.cpp and use this value in b.cpp.
Thanks,
Chris.C
-
Your variable is declared as normal in the first .cpp file. In the second, add an extern definition. For example:
file1.cpp
file2.cpp
Make sense? (I hope so! ;))
-
can I do it in this way?
Code:
file1.h
double pt1;
Code:
file2.cpp
extern double pt1;
-
If file2.cpp includes file1.h, then you don't need to do anything in file2.cpp, since it will have it. However, it's not usually a good idea to declare variables in a header. Unless, of course, it's an extern. In <iostream>, there is a definition of:
...because it refers to one thing all the way through all the code. Normally headers should only contain definitions of classes, types, structures, and as little code as possible. (You can put code in, but be careful)
-
Thanks Parksie. I know how to improve my program already. Thanks a lot for your explaination.