Defines vs. const object

I'm procrastinating a new session of revision and debugging, but there is a nagging question regarding the transition from ANSI C to C++ I have.

I've seen programmers define a const int or floating point and then declare it. Example:

const double Planck = 6.626068e-34

Back when I was programming ANCI C, we would do

#define PLANCK 6.626068e-34

Essentially, neither be changed and represent the same value.

I may(more like probably) be needing to use such constants in the future, so what is the advantage of Plank vs. PLANK?
Last edited on
Whether you put your constants in all uppercase or not is a matter of tastes, but there is literally no reason to use a define here. In this particular situation, you're unlikely to notice much of a difference, but there are other instances in which it matters (e.g. struct/class instances).

but there are other instances in which it matters (e.g. struct/class instances).


That's the kind of info I need to know. Why is there no reason to use a define here? Is it disadvantageous here?

What kind of instances call for a define vs. const object, and vice versa?


Compare

1
2
#define MY_CONST ComplicatedClass();
const ComplicatedClass myConst;


"ComplicatedClass" needs to do something when constructed. myConst is created exactly once, while MY_CONST is inserting a default constructor call to ComplicatedClass every single time. That's quite a huge difference. I don't really know of any situation in which you'd prefer "define" - ing a constant over just using const.
Last edited on
Ty Mos, that is what I was looking for.
Topic archived. No new replies allowed.