For constants values, using const int Changed_num = 100;
has the advantage over #define Changed_num 100
in that you can assign a type. For example, you can const unsigned long Changed_num = 100
, which is a bit tricky to declare as a #define
. (You can do something like #define Changed_num 100ul
, but it's not as obvious.)
One possible use for #define
as part of logging macros, such as boost::log
. They have the advantage of being to interpolate things like __FILE__
and __LINE__
at the point they're called. They are also used for code generation, such as with boost::foreach
(which has been supplanted by the range-based for in c++11
) or boost::python
. In general, however, you're better off using [templated] functions so that you get proper type safety.
The main disadvantage of #define
is that it's a super heavy hammer. If you #define
something, you can't override it later with a local variable or function. One particularly egregious case is Windows.h
which #define
s min
and max
, giving compiler errors if you try to use std:::min
unless you set NOMINMAX
.