I'm expecting the following code to output 'AAA ClassA ClassB', but it's only outputing 'ClassA ClassB'. Is my expectation correct? If not, how can I make sure it's initializing all the static variables.
//main.cpp
#include "AAA.h"
#include "ClassB.h"
#include "ClassA.h"
int main()
{
ClassA::Print();
}
// AAA.h
#pragma once
class AAA
{
public:
static size_t sSize;
};
// AAA.cpp
#include "AAA.h"
#include "ClassA.h"
size_t AAA::sSize = ClassA::Insert( "AAA" );
// ClassA.h
#pragma once
#include <vector>
#include <string>
class ClassA
{
public:
static size_t Insert( const std::string& str );
static void Print();
static std::vector<std::string> sVec;
static size_t sSize;
};
// ClassA.cpp
#include "ClassA.h"
#include <iostream>
std::vector<std::string> ClassA::sVec;
size_t ClassA::sSize = ClassA::Insert( "ClassA" );
size_t ClassA::Insert( const std::string& str )
{
sVec.push_back( str );
return sVec.size();
}
void ClassA::Print()
{
for( const auto& str : sVec )
std::cout << str << std::endl;
}
// ClassB.h
#pragma once
class ClassB
{
public:
static size_t sSize;
};
// ClassB.cpp
#include "ClassB.h"
#include "ClassA.h"
size_t ClassB::sSize = ClassA::Insert( "ClassB" );
When I debugged in VS, sVec did contain "AAA", but then it gets initialized again to be an empty vector. Then "ClassA" and "ClassB" gets inserted.