You are here:

C++/macros in c++

Advertisement


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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.