Quote:
Originally Posted by qcpp
Now, my question is whether it is sufficient to include the file A.h in B.h or i also need to say Class A (as a forward declaration in B.h) in B.h ?
If you just need to use a pointer to A like A *test, then you should use
forward declaration of A. Of course including A.h will also do the job, but will make the compilation process slower. If Class B inherits from A (implicitly or explicitly), then including A.h is a must.
Quote:
Originally Posted by qcpp
When to use the forward declaration ?
Whenever possible, 'cuz it's faster.
Quote:
Originally Posted by qcpp
when is it necessary ?
It's never necessary ! I mean there's nothing that cannot be done without forward declaration.
Quote:
Originally Posted by qcpp
is there any specific conditions ?
Yes, there are. When you forward declare, the compiler has no idea about the contents of the class, it just knows that a class exists. So, you cannot use forward declaration for a class, when you want to access the members of that class.
Some examples, for further clarification. When you forward declare a class A, you can do things like:
- Declaring a reference or pointer to it.
- Declaring a func, whose arg type or return type is that class.
- Declaring a func, whose arg type or return type is pointer/reference to that class.
But you cannot:
- Declare a member of that type.
- Use it as base class.
- Define func, whose return/arg-type is that class.
- De-reference a pointer of the forward declared class.
Quote:
Originally Posted by qcpp
Thanks in Advance..

You are welcome !