I've seen several posts about this issue, but none of them explains well my concern, so I'll try to explain here what I understand and please correct me if I'm wrong.
Suppose I have a header file with the following declaration:
//definitions.h
extern const float fallingTime;
Now, I have two source files that want to use this declaration.
//source1.cpp
#include "definitions.h"
const float fallingTime = 0.5f;
//use fallingTime
//source2.cpp
#include "definitions.h"
//just use fallingTime (no definition required)
This is what I do; but now, assume this other way to proceed.
//definitions.h
const float fallingTime = 0.5f; //Note that I don't use extern now
//source1.cpp
#include "definitions.h"
//just use fallingTime (no definition required)
//source2.cpp
#include "definitions.h"
//just use fallingTime (no definition required)
As I concluded from reading several sources, the advantages of the former approach is that it saves memory and compilation time, because memory allocation only occurs one (in the definition in source1.cpp), whereas in the latter approach memory allocation happens in every source file that includes definitions.h (source1.cpp and source2.cpp). Is that correct?
Finally, what would imply using extern and defining the constant at the same time? Would be equivalent to the former approach?
//definitions.h
extern const float fallingTime = 0.5f;