#define ?
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
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.
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.
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.
#defines are troublesome; replace them with inline functions and const variables whenever possible.
There''s nothing wrong with defines. Defines have their place, consts have their place, inline functions have their place; learn when each is best.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement