Assume these two cpp files:
0.cc
#include <iostream>
class test{
public:
int num = 5;
};
int main(){
test t;
return t.num;
}
1.cc
class test{
public:
int num = 6; // <-- note this has a different value.
};
We compile it with g++ 0.cc 1.cc
.
Initializing a member variable inside the class works since C++11. My question is how is it compiling whiteout a linker error? is it undefined behaviour? as we are breaking the one definition rule. If classes were not used this would generate a link error at compile time.
I know that defining a function within a class makes it inline so it would be ok to have multiple of them in different files, but not sure how multiple variables are ok?