To demonstrate forward declarations -
I put up a simple structure consisting of 3 source files main.cc a.cc and b.cc
a.h
====
#include <iostream>
class A {
public:
void show();
};
I use a forward declaration of class A in b.h
b.h
====
#include <iostream>
class A; // forward declaration - no #include "a.h" here
class B {
public:
void showA(A *ptrA);
};
a.cc
=====
#include "a.h"
void A::show()
{
std::cout << "Demonstrates minimum compilation dependencies";
}
By using the forward declaration in b.h effectively the #include "a.h"
has been moved from b.h to b.cc
b.cc
=====
#include "b.h"
#include "a.h"
void B::showA(A *ptrA)
{
std::cout << "Calling show method of A" << std::endl;
ptrA->show();
}
And finally...
main.cc
=======
#include "a.h"
#include "b.h"
int main()
{
B *ptrB = new B;
A *ptrA = new A;
ptrB->showA(ptrA);
return 1;
}
Effectively by using a forward declaration I have moved the #include "a.h" from b's header to the source file. How does the compilation dependency reduce ?
Cheers
I put up a simple structure consisting of 3 source files main.cc a.cc and b.cc
a.h
====
#include <iostream>
class A {
public:
void show();
};
I use a forward declaration of class A in b.h
b.h
====
#include <iostream>
class A; // forward declaration - no #include "a.h" here
class B {
public:
void showA(A *ptrA);
};
a.cc
=====
#include "a.h"
void A::show()
{
std::cout << "Demonstrates minimum compilation dependencies";
}
By using the forward declaration in b.h effectively the #include "a.h"
has been moved from b.h to b.cc
b.cc
=====
#include "b.h"
#include "a.h"
void B::showA(A *ptrA)
{
std::cout << "Calling show method of A" << std::endl;
ptrA->show();
}
And finally...
main.cc
=======
#include "a.h"
#include "b.h"
int main()
{
B *ptrB = new B;
A *ptrA = new A;
ptrB->showA(ptrA);
return 1;
}
Effectively by using a forward declaration I have moved the #include "a.h" from b's header to the source file. How does the compilation dependency reduce ?
Cheers