Archived

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

Mat1515

#define ?

Recommended Posts

Im using Visual C++ 6.0 I have multiple .cpp files being linked together for my project. If I were to say #define Apple 1 //in Main.cpp Will this defintion remain throughout the whole project or would I have to put this in a header file and include it to each file? Thanks Matt

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Defines are really great, use them when ever possible!

Share this post


Link to post
Share on other sites
It will only be there for the file you define it in, so you''d have to put in a header file and include it in all your project files if you wanted them all to use it.

Share this post


Link to post
Share on other sites
You seem to know the answer to this already, and a simple test would have told you your second choice is correct.

cpp files are pretty dumb, they don''t know about anything you don''t tell them directly with header files (generally).

Including header files is simply text replacement. When you include a header file, all the text in the header file gets inserted into your cpp file before compilation. (this is done by something called the preprocessor). If you define apple in a header and include it in your cpp, define simply gets inserted where you called include, and everything below that include now knows about apple. It''s like you took all the text in header.h (or whathaveyou) and copied into you cpp file.

Share this post


Link to post
Share on other sites
There''s nothing wrong with defines. Defines have their place, consts have their place, inline functions have their place; learn when each is best.

Share this post


Link to post
Share on other sites
quote:
Original post by AndyOxfeld
There''s nothing wrong with defines. Defines have their place, consts have their place, inline functions have their place; learn when each is best.

Sure, but don''t use #defines where an inline function or const identifier would work equally well: #defines have enough problems associated with them to discard them unless they are demonstrably better and not merely functionally equivalent. See Stroustrup''s FAQ, for example.

Share this post


Link to post
Share on other sites