I came across a piece of advise in stack overflow which mentions about When to use and when not to user forward declaration .
I came across this :-
struct X; // Forward declaration of X
void f1(X* px) {} // Legal: can always use a pointer/reference
X f2(int); // Legal: return value in function prototype
void f3(X); // Legal: parameter in function prototype
void f4(X) {} // ILLEGAL: *definitions* require complete types`
The last line Which says illegal will fail while compilation.
void f3(X); // Works perfectly fine
So is it that all header files (.hh) are first scanned by compiler and then all
.cc file looked for syntax and symantics where we actually can define the
void f3(X); as after scanning through header files compiler will have idea
about X its member function and member
The compiler doesn’t scan header files. All that happens is that the preprocessor copy-and-pastes the contents of a header file whenever a
#includeis encountered, before handing that source file to the compiler.So after the preprocessor has run, this example:
foo.h
bar.c
simply becomes:
and that is what the compiler itself operates on.