Suppose that I have 3 .cpp
files. A.cpp
, B.cpp
and C.cpp
.
Inside those files there are classes (see code below) and after each class an object is initialized.
File A.cpp
.
class A
{
public:
A()
{
std::cout << "A constructor\n";
}
};
A a;
File B.cpp
.
class B
{
public:
B()
{
std::cout << "B constructor\n";
}
};
B b;
File C.cpp
.
class C
{
public:
C()
{
std::cout << "C constructor\n";
}
};
C c;
Finally, we build using cmake
like this:
add_executable(${PROJECT_NAME} src/main.cpp src/A.cpp src/B.cpp src/C.cpp)
I know that the constructor will be called once an object is initialized. In that case, there are 3 objects that are initialized inside a .cpp
file.
Is there any predefined order that the objects are initialized, therefore a specific order that the constructors are called?
I know I can run the program and figure it out by myself but I'd like to know if there is any rule that applies to this case.
I'm developing some code for embedded systems and I wanna be sure that I won't run into a race condition.