Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling class name in the header file

Tags:

c++

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!

like image 661
domlao Avatar asked Aug 26 '09 00:08

domlao


People also ask

How do you define a class in a header file?

Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a . cpp file of the same name as the class. Now any other header or code file that wants to use the Date class can simply #include "Date. h" .

How do you call a class name in C++?

A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.

How do you call a header file?

You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a '. h' an extension. By including a header file, we can use its contents in our program.

Do classes go in header files C++?

C++ classes (and often function prototypes) are normally split up into two files. The header file has the extension of . h and contains class definitions and functions.


1 Answers

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.

like image 58
Walt W Avatar answered Oct 05 '22 07:10

Walt W