Let's assume that we have a source file A.cpp where we forward declare a type ClassB, and then we keep using pointers to ClassB without ever #including file B.cpp (where ClassB is defined); And in B.cpp we forward declare ClassA and use a pointer to it without ever #including A.cpp (where ClassA is defined), then is the compiler fully happy with that? and will symbols resolution work just fine? In other words, do these two object files need not know about one another at all before link-time?
(I am assuming compiling C++ code on visual studio without any changes to the default compiler)
PS:
File A.cpp
class ClassB;
class ClassA
{
bool JustTakeAClassBPointAndDoNothingWithIt(ClassB* class_b_pointer)
{
if(class_b_pointer)
return true;
else
return false;
return false;
}
}
File B.cpp
class ClassA;
class ClassB
{
bool JustTakeAClassAPointAndDoNothingWithIt(ClassA* class_a_pointer)
{
if(class_a_pointer)
return true;
else
return false;
return false;
}
}
This questions is really too general to answer properly, but here's my 2 cents. In general, as long as you only refer to a class as a pointer to it, compilation will work. eg this example compiles fine:
class B;
int main() {
B * tst;
return 0;
}
However, as soon as you try to actually instantiate a the pointer, or access any of its methods, you need a full definition. Those examples will NOT work:
class B;
int main() {
B * tst = new B(); // error: allocation of incomplete type 'B'
return 0;
}
Or:
class B;
int main() {
B * tst;
tst->print(); // error: member access into incomplete type 'B'
return 0;
}
tl;dr; As long as you don't actually interact with it, you can use an incomplete type. If you use any method or function, you need to declare them in advance (including constructors)