Is some one able to explain why header files have something like this?
class foo; // This here?
class bar
{
bar();
};
Do you need an in
This is called forward declaration. The body of the class foo
would be defined at a later part of the file. Forward declaration is done to get around cyclic dependencies: The definition of class Bar requires class Foo and vice versa.
class Bar
{
Foo * foo;
};
class Foo
{
Bar * bar;
};
As you can see, Bar
has a reference to Foo
and vice versa. If you try to compile this, the compiler will complaint saying that it doesn't know anything about Foo
. The solution is to forward declare the class Foo
above the Bar
(just like you declare the prototype of a function above the main
and define its body later).
class Foo; //Tells the compiler that there is a class Foo coming down the line.
class Bar
{
Foo * foo;
};
class Foo
{
Bar * bar;
};