C++/macros in c++
Expert: vijayan - 8/18/2008
Question
Hello, Vijayan
what is the purpose of macros in c++?
and how can i use a macro from another file/program?
thanks
Answer> what is the purpose of macros in c++?
macros are a type-unsafe feature of the C preprocessor and are considered avoidable in most situations by by C++ experts. see:
http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5
in general, the thumb-rule is if there is an equivalent compiler feature available, prefer that to a preprocessor technique.
places where macros are un-avoidable are places where you need to refer to tokens in your source file. a good example is the assert macro in <cassert> which needs to know about the file name, line number and the text of the assertion.
> and how can i use a macro from another file?
only by #including the file; remember, it is a preprocessor technique.
> and how can i use a macro from another program?
obviously this is not possible other than by using the source file (typically a header) that defined the macro.