C++/Public variables accross source files within a program
Expert: Zlatko - 4/28/2010
QuestionHi again, Zlatko.
I am building a program that analyzes certain aspects of a file and writes a graph showing its findings.
I would like to utilize an array in two CPP implementation/source files within the program: LinePlot.cpp and LinePlotTestDlg.cpp. Is there any way to make this array 'public' within the two CPP implementation files?
Thanks in advance for reading, analizing and answering my question,
-Neil
AnswerHello Neil.
There is no strict way to create a global variable in file A, and have it visible to only one other file, say file B. For a variable in file A to be visible to file B, file B must have an extern declaration for the variable. You can limit the visibility of the variable by keeping its extern declarations out of any header files, and putting the extern declaration only in the files that need access to the variable. If you do that, and if you ever change the type of the variable, you would have to search out all the externs, and change them too. I don't like the method for that reason.
If your variable is a private class member in class A, then you can let class B see the variable by specifying that class B is a friend of class A. You would specify the friendship in class A.
I hope that helps.
Best regards
Zlatko