1

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?

1 Answer 1

1

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++.

Sign up to request clarification or add additional context in comments.

7 Comments

That's not what const means. You can have const int x = rand(); just fine.
I wouldn't call that "being more general", rather "not being wrong". const values don't have to be compile-time constants.
@Magtheridon96 and is it compatible with C++03 and not platform-specific?
@LuchianGrigore i need only global compile-time constants, otherwise I couldn't implement them with macros
@Amomum okay, what I said was that const doesn't restrict variables to compile-time constants.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.