Reputation: 93
#ifndef PC
#define PC
#include <iostream>
#include <string>
#include "../include/worm.h"
#include "../include/dns.h"
class DNS; // Forward decleration
class PC
{
....(there is all the declration here)
};
#endif
why is there the "class DNS;"? what is the purpose of writing that?(if you need more of the code to understand the need\purpose let me know)
Upvotes: 4
Views: 2670
Reputation: 1520
We would need more code, specifically DNS's header file, but I'm assuming dns.h also includes pc.h. When you have two header files that include each other, they must each forward declare the other class.
However, if dns.h does not include pc.h, then you don't need the forward declaration.
My understanding of it involves circular dependencies. Imagine I'm walking through dns.h, and I see pc.h. I need to know what you depend on, so now I start going through pc.h. I now see dns.h, but I was just there so I keep going through pc.h. If you use DNS inside of class PC, how am I to know what DNS is? I stopped walking through dns.h before I hit that class declaration.
The solution is to foreward declare the class in pc.h. This gives me just enough information to know that DNS is a class of some sort, and to not freak out when I see arguments, variables, etc. of type DNS. I now have confidence DNS will be defined later.
Upvotes: 4