Archived

This topic is now archived and is closed to further replies.

Tazel

Headers Function how?

Recommended Posts

Tazel    122
When you have something in a header like a class, can you use the public members of it if you just include it on the top of the source file?
tcache Contact Me
Formerly known as Wachar <- Thrander <- Tazel

Share this post


Link to post
Share on other sites
Johnny Watson    508
The #include directive works by taking the contents of the header file and putting them in the place of the call to the #include directive. For example...


  
//Whatever.h


enum EYE_COLOR
{
EC_BROWN,
EC_GREEN,
EC_BLUE
};

struct PERSON
{
char Name[256];
int Age;
EYE_COLOR EyeColor;
};

//Whatever.cpp


#include "Whatever.h"

int main()
{
//...




  
//Whatever.cpp, when run through the preprocessor is converted to:


enum EYE_COLOR
{
EC_BROWN,
EC_GREEN,
EC_BLUE
};

struct PERSON
{
char Name[256];
int Age;
EYE_COLOR EyeColor;
};

int main()
{
//...



So, to answer your question, yes, you can, seeing as how it will just be as if it were defined at the top of the program at linking time. You have to be careful though, if you defined an instance of the PERSON object, for example, you''d be in trouble because the global would be defined multiple times if you included it in multiple source modules. Also, if you recursively include things, it can lead to further complications.

Share this post


Link to post
Share on other sites
Tazel    122
Ok thanks. Also, is it better to initialize headers like windows.h in the header file and just include your one own header file or just initialize all of the windows headers in the cpp file?


tcache
Contact Me


Formerly known as Wachar <- Thrander <- Tazel

Share this post


Link to post
Share on other sites