Advertisement

File Splitting (Externs/Headers)

Started by February 02, 2000 03:42 PM
14 comments, last by Ishamael 24 years, 8 months ago
TOTALY AGREE, CAUSE DAT WHAT I DO
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...
Advertisement
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.
Thank you! Thank you so much! After a LOT of blood, sweat, and tears, it finally works! Thank you so much! I used MajorD''s example, and it works! I''m SO happy! Thank you!
Glad that it worked for you.

This topic is closed to new replies.

Advertisement