What is the cost of #define?

To define constants, what is the more common and correct way? What is the cost, in terms of compilation, linking, etc., of C # define constants? Is this another way cheaper?

+5
source share
7 answers

The best way to define any const is to write

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

Usage #defineis bad C ++ style. Cannot be hidden #definein namespace scope.

Comparison

#define pi 3.1415926

from

namespace myscope {
const float pi = 3.1415926f;
}

The second way is obviously better.

+14
source

#define. . , ... .

#define SOME_STRING "Just an example"

, . , , , , . const, .

+5

, #defines ( , #defines , ).

+2

#define , . , -, .

const .

const unsigned int ArraySize = 100;

static const unsigned int ArraySize = 100;

.

+1

#define , ...

#define...

const

...

+1

#define - . , , , . .

. , , const .

0

CPU - #define . "" :

  • , , , , .
  • .
  • .

, .

const, .

0
source

All Articles