ClassA has a member pointer to classB. I also added forward declaration of ClassB as a reference for the pointer b. I don't have #include "ClassB.h" in ClassA.h and ClassA.cpp. This code builds without error.
I'm confused how forward declaration alone is enough to successfully compile this code? If header is not required, what is the reference of the forwarded class?
I checked the other possible duplicates but it doesn't give clarity to my confusion since i'm not asking to fix the code here.
Can any one give clarity to my confusion.
ClassA.h
#ifndef CLASS_A
#define CLASS_A
#include <string>
class ClassB;
class ClassA
{
ClassB *b;
};
#endif
ClassA.cpp
#include "ClassA.h
ClassB.h
#ifndef CLASS_B
#define CLASS_B
#include <vector>
#include "ClassA.h"
class ClassB
{
std::vector<ClassA> a;
};
#endif
ClassB.cpp
#include "ClassB.h"
ClassA.h+ClassA.cppcompile intoClassA.o(or whatever), theClassB* bmember variable is of a known size. It only takes up a pointer, which is the same for all object pointers. - JohnFilleau