I know global is bad but just as a practice, is this the correct way to initialize a global class used between multiple object files?
Header 1.h
class test {
int id;
public:
test(int in){
id = in;
}
int getId(){
return id;
}
};
extern test t;
File 1.cc:
#include <iostream>
#include "1.h"
int main(){
std::cout << t.getId() << std::endl;
return 0;
}
File 2.cc:
#include "1.h"
test t(5);
Now what if instead of extern
I use the static
approach globally static test t(0);
in the header?
Correct me if I'm wrong but that would compile fine however I would have 2 different unrelated copies of the same t
in both object files and the final binary? Is that bad? Or does the linker sort it out to eliminate multiple copies?