After solving this simple issue , I had to ask :
-> In the H file in a class ex a const static member is defined, eg :
class ex {
const static int my_ex;
};
-> In the CPP file the value is specified
ex::my_ex = 32;
And then one gets the error "conflicting declarations" (as well as "does not name a type"). I understand that the definition in the CPP file is also a declaration which does create a conflict seen from the linker BUT why only about the const specifier (and type) and not the static one ? I only have to write
const int ex::my_ex = 32;
to get it to compile. But not static ... Why not? Why can't I just define and not repeat declaration related steps (type, specific identifiers)?
This is a historical thing.
Since C, static
on a definition means "internal linkage". When C++ came along, and classes were added to it, Bjarne needed a keyword to signify static members. Not wanting to add new keywords (a preference that largely exists still to this day), he re-used static
instead.
Now static
meant two different things depending on where you put it. So you can't write static
here because it would mean something else. Thus, the language doesn't require you to, as that would be silly.
Beyond that reasoning, it just is . When you create a language, you balance simplicity of specification against simplicity of implementation against simplicity of use, and you come up with a set of rules that are the language. At some point you have to stop quibbling over "why" some insignificant rule was created and just get on with writing your program.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.