So I’m working on a project that has two const arrays of two classes (classes A
and B
). The class A
array needs access to the class B
array while initializing, but the B
array always returns nullptr
, which is a big problem. Perhaps it would be easier to see the code (heavily simplified because the actual classes are huge and most of the info is irrelevant here):
// A.h
#pragma once
#include <vector>
#include "B.h"
#include "B_ARRAY.h"
class A
{
private:
int bOffset; // the array offset of the corresponding element in B_ARRAY
std::vector<int> otherInfo; // other info, which determines the corresponding B_ARRAY element
public:
A(std::vector<int> i) : otherInfo(i)
{
bOffset = -1;
while (bOffset == -1)
{
// search algorithm for the corresponding element in B_ARRAY
}
}
B* getB()
{ return B_ARRAY[bOffset]; }
}
// B.h
#pragma once
#include <vector>
class B
{
private:
std::vector<int> info;
public:
B(std::vector<int> i) : info(i)
{}
std::vector<into> getInfo()
{ return info; }
}
// B_ARRAY.h
#pragma once
#include "B.h"
const int B_LENGTH = 900;
B* const B_ARRAY[B_LENGTH] =
{
new B(std::vector<int>({...})),
new B(std::vector<int>({...})),
[...]
};
// A_ARRAY.h
#pragma once
#include "A.h"
const int A_LENGTH = 1200;
A* const A_ARRAY[A_LENGTH] =
{
new A(std::vector<int>({...})),
new A(std::vector<int>({...})),
[...]
};
While it's searching in the A
constructor, B_ARRAY[x]
is always nullptr
, which makes B_ARRAY[x]->getInfo()
fail. In main()
, it works just fine, so I assume this means B_ARRAY
isn't fully initializing until main()
. What do I have to do to make sure it fully initializes before A_ARRAY
?
I could make a B
object as a member of the A
class, but a lot of A
classes end up with identical B
s (in my current code, I'd want them to reference the same B
object), and both of the actual constructor calls are already long enough as it is, so splitting them over two arrays helps keep it organized.