from this question (still not solved) I come across this new problem,so here I made an example:
//main.cpp
int main() {
return 0;
}
//file1.cpp
#include "b.h"
B b;
//file2.cpp
#include "a.h"
A a;
//a.h
#pragma once
#include<iostream>
#include "b.h"
extern B b;
class A
{
public:
A(){ std::cout << "a cotr" << std::endl;b.Use(); }
};
//b.h
#pragma once
#include<iostream>
class B
{
public:
B() { std::cout << "b ctor"<<std::endl; };
void Use() { std::cout << "use b" << std::endl; }
};
In g++ 6.3.0 the output is:( g++ -o test file1.cpp file2.cpp prac.cpp -std=c++11)
a cotr
use b
b ctor
So from the code example it seems that there's no such guarantee,and probably is an undefined behavior ?Is there anywhere the standard said about this?(I do not think it's a duplicate because this situation is a little different:in the initialization of a
,invoke b
's member function.)