I have a class and it has an integer value that will be used by other classes as well as the original class. And it's a fairly simple value (e.g. the dimensions of a GUI application). In this case, should I define these values as static const int VALUE = 123
or #define VALUE 123
in my header file? Does one have any advantage over the other?
Asked
Active
Viewed 87 times
-2

Mertcan Ekiz
- 691
- 1
- 9
- 22
-
That depends, there are cases where `static const` values might turn out less efficient than a simple `#define`. – πάντα ῥεῖ Nov 15 '14 at 12:51
-
http://stackoverflow.com/questions/1637332/static-const-vs-define – theonlygusti Nov 15 '14 at 12:53
-
1you should also consider constexpr – stefan Nov 15 '14 at 13:02
1 Answers
2
When you're using C++ you almost never need to use #define
. (Only with include guards)
#define
knows nothing about semantics of code. It simply replaces text VALUE
with defined value.
static const int
is much better choice then. It is recommended way to use constants.

justanothercoder
- 1,830
- 1
- 16
- 27