In the specific case of const variables in the global or namespace scope, you can just directly declare the object like how it is in the original post in the header file, because the const variables will implicitly have internal linkage.
But the above way is more general and will work with const and non-const objects alike.
From C++17 there is inline which makes this easier for non-const variables:
An inline function or variable (since C++17) with external linkage (e.g. not declared static) has the following additional properties:
There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit and (for non-static inline functions and variables (since C++17)) all definitions are identical. For example, an inline function or an inline variable (since C++17) may be defined in a header file that is #include'd in multiple source files.
Thank you all already for the answers, is there also a way to store them in struct just like this:
{"maj", {0,4,7}},
So without declaring a map? Cause maybe in the future i want to use them in another way.
(a bit the idea of an xml file).
For example, i got the example from here:https://github.com/BespokeSynth/BespokeSynth/blob/main/resource/userdata_original/scales.json
json, xml are special text file formats. C++ can use them, but not 'directly' or internally as part of the code: you have to extract them (when reading a file, or similar) into data structures (like a map, or a struct, or vector etc) to really make use of them for anything nontrivial.
you can certainly store them in a struct.
consider?
1 2 3 4 5 6 7 8
enum chordtype{maj,min,aug,aug7th, ..?.., max_chordtype};
string chordtypenames[max_chordtype]{"maj","min", ... };
struct chord
{
chordtype type; //you can keep this as string if you want, the enum/string pair idea
//may be more flexible: its just an example of an alternate way to store it (its like a map)
int numbersofsomeuncleartype[3]; //this needs a meaningful name
};
and then you can make a container of the above as a vector, map, tree, whatever you want...