Amomum
Amomum

Reputation: 6483

Using constants instead of macros

Is it possible to replace macros with constants, if macro is defined with another macro like this:

#define START_OFFSET    0
#define ADDRESS_OFFSET  (START + START_SIZE)
#define SIZE_OFFSET     (ADDRESS_OFFSET + ADDRESS_SIZE)
and so on

I'm not entirely sure, what will happen if I use global constants and initialize them with constants. Can that be considered safe?

The reason for using constants is for the possibility to wrap them into namespace. Btw, I'm using macros like these only for working with messages which are stored in byte arrays.

Is structure serialization is better option?

Upvotes: 1

Views: 167

Answers (1)

user123
user123

Reputation: 9071

I'm not entirely sure, what will happen, if I use global constants and initialize them with constants. Is it safe?

Yes, that's fine.

const int i = 4;
const int j = 6;
const int k = i + j; // legal

Is structure serialization is better option?

It depends on what you want to accomplish. Right now, that question is slightly broad. There are no golden hammers in C++.

Upvotes: 1

Related Questions