tags:

views:

89

answers:

3

Hi,

I just stumbled a c++ code with a calling of a class name in the upper part of the header file for example

class CFoo;
class CBar
{
  ....
};

My question is, what is class CFoo for?

Thanks alot!

+10  A: 

This is called a forward declaration. It means that there IS a class named CFoo, that will be defined later in the file (or another include). This is typically used for pointer members in classes, such as:

class CFoo;
class CBar {
    public:
        CFoo* object;
};

It is a hint to the C++ compiler telling it not to freak out that a type name is being used without being defined, even though it hasn't seen the full definition for CFoo yet.

Walt W
A: 

It's called a forward declaration.

http://en.wikipedia.org/wiki/Forward%5Fdeclaration

ThePosey
+1  A: 
class CFoo;

Is just a declaration that the class exists; even if you haven't seen the definition yet, you can still play with (CFoo *) or (CFoo &) - that is, pointers and references to CFoo.

wrang-wrang
You can also define functions that return CFoo (by value).
Tom
Really? That seems unlikely to me, because to return by value you'd need to know at least the size of CFoo.
wrang-wrang