|
-
January 28th, 2009, 04:28 AM
#1
Value of Global variable not consistent
I have created a program wide global variable in a .h file called globals.h
Inside the .h file, the global variable is declared as:
extern float g_fFrameTime;
2 other classes use this global variable.
I included globals.h in each of these classes and declared a "float g_fFrameTime;" private data member in these 2 classes.
The first class assigns a value to g_fFrameTime via
g_fFrameTime = GetFrameTime();
while the second class uses the value of g_fFrameTime to perform some calculations.
However, when I printed out the value of g_fFrameTime of these 2 classes into 2 seperate text files respectively, their values differ. The first class which assigns a value to the global variable prints the correct value to the file (values hovering around 0.01) while the second class which is supposed to use the value of g_fFrameTime prints different values to another file (values around -9.xxxx).
Besides these 2 classes, no other classes assigns values to or uses the global variable.
What is the problem causing this inconsistency?
-
January 28th, 2009, 04:31 AM
#2
Re: Value of Global variable not consistent
 Originally Posted by ZhiYi
I included globals.h in each of these classes and declared a "float g_fFrameTime;" private data member in these 2 classes.
...
What is the problem causing this inconsistency?
A private data member is not a global variable. Effectively, you declared three variables named g_fFrameTime. One is in global scope (but apparently was never defined and used), the other two are in the scope of their respective classes. If you really want to use a global variable, define it in exactly one source file, in global scope.
-
January 28th, 2009, 04:36 AM
#3
Re: Value of Global variable not consistent
No wonder the values are so strange.
Is declaring an extern float in a .h file and including the header file in the 2 classes the correct method to use that global variable?
-
January 28th, 2009, 04:39 AM
#4
Re: Value of Global variable not consistent
 Originally Posted by ZhiYi
Is declaring an extern float in a .h file and including the header file in the 2 classes the correct method to use that global variable?
Yes, but as I noted the variable needs to be defined (not declared extern) somewhere in a source file. Also, if it is feasible to avoid the global variable, then you should consider not using one.
-
January 28th, 2009, 04:43 AM
#5
Re: Value of Global variable not consistent
 Originally Posted by ZhiYi
No wonder the values are so strange.
Is declaring an extern float in a .h file and including the header file in the 2 classes the correct method to use that global variable?
One and only one source file must declare the global variable without "extern". All other source files must declare the global variable as "extern".
Also, your description is not clear, and an actual code example from you would explain what you're doing. What do you mean by "private variable"? A variable that is declared outside the scope of any function is either global, or a static variable that is only known to the module that is being compiled. There is no such thing as a "private" global variable.
Regards,
Paul McKenzie
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
|