Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cost of a #define?

To define constants, what is the more common and correct way? What is the cost, in terms of compilation, linking, etc., of defining constants with #define? It is another way less expensive?

like image 837
emenegro Avatar asked May 24 '10 07:05

emenegro


1 Answers

The best way to define any const is to write

const int m = 7;
const float pi = 3.1415926f;
const char x = 'F';

Using #define is a bad c++ style. It is impossible to hide #define in namespace scope.

Compare

#define pi 3.1415926

with

namespace myscope {
const float pi = 3.1415926f;
}

Second way is obviously better.

like image 143
Alexey Malistov Avatar answered Oct 05 '22 23:10

Alexey Malistov