File Splitting (Externs/Headers)
About statics... If you declare a static variable, it is only visible inside that source file, and other sources can declare variables with the same name but a different instance. So, if you declare something static in a header, I think every source file which includes that header will have a separate instance of that variable. Not what was intended to be a globally unique instance.
To Tuna, if you have a global class object, a good idea is to extern it in the same header where the class''s declaration is. Another alternative is to prototype the class:
myclass.h:
class MyClass { ... };
globals.h:
class MyClass;
extern MyClass myGlobalInstance;
This of course helps only if every source file concerned includes both myclass.h and globals.h, but the order wouldn''t matter then...
To Tuna, if you have a global class object, a good idea is to extern it in the same header where the class''s declaration is. Another alternative is to prototype the class:
myclass.h:
class MyClass { ... };
globals.h:
class MyClass;
extern MyClass myGlobalInstance;
This of course helps only if every source file concerned includes both myclass.h and globals.h, but the order wouldn''t matter then...
If the globals.h is to be declared for external variables, then how does the #ifndef in that file help? Aren''t external declarations safe for multiple declarations?
extern declarations are safe from multiple declarations, but that''s no reason to get sloppy. And who knows? One day you might want to use globals.h for something else.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement