2

I initialize two extern const variables in this way:

vars.hpp:

int init_a();
extern const int a;
extern const int b;

vars.cpp:

#include<vars.hpp>
int init_a() {
    return 1;
}
const int a = init_a();
const int b = 1;

stuff.hpp:

extern const int d;
extern const int e;

stuff.cpp:

const int d = a;
const int e = b;

main.cpp:

#include<vars.hpp>
#include<stuff.hpp>

int main(){
    std::cout << a << b << d << e << std::endl;
return 0;
}

The output is "1101", while I expect "1111". Could you please explain why value "d" is not shared among stuff.cpp and main.

If it is an normal initialization order problem, as far as I know, It should be 1111 (initialize vars.cpp first) or 1100 (initialize stuff.cpp first).

Meng Sun
  • 21
  • 3

0 Answers0