Header File Questions....
Alright here is my question:
I have a header file:
/*
==============
HEADERFILE
==============
*/
extern char testChar;
...
---------------------------------------------
Now I include that headerfile in my source code whatever.cpp, why is it that I would declare
char testChar;
isn''t testChar already declared as a global in the headerfile?
ALSO, lets say I am working on a larger project why does the use of static make functions and variables private?
THANKS,
Chris
Extern just means "I''m using this variable but it''s declared somewhere else".
So you do have to declare it somewhere in a .cpp file. All the other .cpp files can simply use your include file.
So you do have to declare it somewhere in a .cpp file. All the other .cpp files can simply use your include file.
errrr ....
extern char TestChar;
is a declaration. That declares a char variable named TestChar. I think what Torn means is that the variable is defined elsewhere ...
As for the other question about static - I don''t know why ... perhaps rather than add a new keyword to the language they decided to overload it. Not my first choice...
Static at file scope means "private to this file" for functions and variables. Static at function scope means "global variable private to this function". Actually, I''m not sure about declaring a file scope variable static - they are private anyway unless there is an extern reference. What happens if you use extern in one file and static in the other ... nuclear meltdown perhaps
With C++, static also means class variables and functions. I guess this is a reasonable use, as virtual functions are dynamic ...
Anyway, if it bothers you, you can use macros:
#define PUBLIC
#define PRIVATE static
but then of course you run into problems with prototypes and variable definitions, so forget that suggestion
--
extern char TestChar;
is a declaration. That declares a char variable named TestChar. I think what Torn means is that the variable is defined elsewhere ...
As for the other question about static - I don''t know why ... perhaps rather than add a new keyword to the language they decided to overload it. Not my first choice...
Static at file scope means "private to this file" for functions and variables. Static at function scope means "global variable private to this function". Actually, I''m not sure about declaring a file scope variable static - they are private anyway unless there is an extern reference. What happens if you use extern in one file and static in the other ... nuclear meltdown perhaps

With C++, static also means class variables and functions. I guess this is a reasonable use, as virtual functions are dynamic ...
Anyway, if it bothers you, you can use macros:
#define PUBLIC
#define PRIVATE static
but then of course you run into problems with prototypes and variable definitions, so forget that suggestion

--
Games, Anime and more at GKWorld
Meet Bunny luv'' and the girls, ready to perform on your desktop just for you (warning: adult content).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement