Forward declarations are useful in a few scenarios:
For example, consider these two classes:
class A {
B* b;
// ...
};
class B {
A* a;
// ...
};
This code won't compile, because A
needs to know about B
, and B
needs to know about A
. We can fix this with a forward declaration:
class B; // Forward declaration
class A {
B* b;
// ...
};
class B {
A* a;
// ...
};
Now A
knows that B
exists (but not its full definition), which is enough for it to have a B*
 member.
Answers to questions are automatically generated and may not have been reviewed.
Learn the basics of writing and using functions in C++, including syntax, parameters, return types, and scope rules.