You probably want to declare Andrew as "extern" variable and define it in some .cpp file - now you will have one Andrew per every file in which you include your header:
namespace ns {
struct Person {
const int id;
};
extern const Person Andrew;
}
// some.cpp
#include "some_file.h"
const ns::Person ns::Andrew = { 2, QStringLiteral("Some string") };
namespace ns {
struct Person {
const int id;
const QString name;
};
extern const Person Andrew;
}
// some.cpp
#include "some_file.h"
const ns::Person ns::Andrew = { 2, QStringLiteral("Some string") };
To copy to clipboard, switch view to plain text mode
Such global data will be initialized before entering main(), so there will be no multiple threads trying to access Andrew at this point.
As a side note, if you want to be sure that something gets called exactly once, you can use std::call_once.
Bookmarks