|
-
Nov 17th, 2000, 08:33 AM
#1
Thread Starter
PowerPoster
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
-
Nov 17th, 2000, 06:47 PM
#2
Monday Morning Lunatic
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! )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 17th, 2000, 11:13 PM
#3
Thread Starter
PowerPoster
can I do it in this way?
Code:
file1.h
double pt1;
Code:
file2.cpp
extern double pt1;
-
Nov 18th, 2000, 09:12 AM
#4
Monday Morning Lunatic
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)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 18th, 2000, 09:38 PM
#5
Thread Starter
PowerPoster
Thanks Parksie. I know how to improve my program already. Thanks a lot for your explaination.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|