KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Multiple definition of a global variable is not allowed in C or C++ due to the One Definition Rule. However, in C++ a const global variable can be defined in multiple compilation units with no error. This is not the same as in C. Why does C++ allow this while C does not? Why does the usage and behaviour of a const global differ from a non-const global in this way in C++ compared to C? What is happening under the covers with C++ and C with respect to const? For example this is allowed in C++, but wrong in C: // Foo.cpp const int Foo = 99; // Main.cpp const int Foo = 99; int main() { cout << Foo << endl; return 0; } And this is fine with C, but wrong with C++: // Foo.cpp const int Foo = 99; // Main.cpp extern const int Foo; int main() { cout << Foo << endl; return 0; }
Tags (comma-separated)
Save Edits
Cancel