as you know, constants defaults to internal linkage.
const int Buf = 1000; // defaults to internal linkage
Buf can be defined in a header file, it's visible only within the files where it is defined and cannot be seen at link time by other translation units.
however, if some complicated structure constants are defined as below:
- constants.h
const complicatedClass myObject("I'm a const object","internal linkage",5);
complicatedClass definition:
class complicatedClass
{
private :
char* charArry;
std::string strTemp;
static int numbers;
int mSize;
public:
complicatedClass();
complicatedClass(char* pChrArry, std::string temp, int size);
~complicatedClass();
public:
void print() const;
std::string getStrTemp() const;
};
it seems that compile must create storage for complicated structure constants, thus it should be external linkage. however, everything is ok when this constants header file (constants.h) was included in multiple files. I assume the linker error should be raised, myObject shouldn't be defined in many places(in multiple files)
can anyone explain this issue? thanks in advance.