LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Difference between including a header file and 'class' declaration in C++ (http://www.linuxquestions.org/questions/programming-9/difference-between-including-a-header-file-and-class-declaration-in-c-228455/)

scoTtkIm 09-09-2004 01:08 AM

Difference between including a header file and 'class' declaration in C++
 
Hello guys,

I am a newbie who is learing C++ and have a simple question.

What's the difference between including another header file and putting a 'class' declaration in C++?

for example,

---------------------------------------------------------------------------------
There are 2 files, a.cpp and b.cpp. Each of these have their own header files, a.h and b.h as well. I want to use class b which is defined in b.h file.

case 1: use include like

include <b.h>

case 2: use class declaration in a.h like

class b;
---------------------------------------------------------------------------------

What is the major difference in these two statements?

phoenix7 09-09-2004 04:23 AM

Hi
Then main difference between forwarding declearion and including or putting class decleration.

This is a forwarding declearion
Code:

class X;
This is a class decleration
Code:

class X
{
...
}

And this is a include !
Code:

#include <x.h> (usually x.h is used for class X)
When you put class decleration on top of your program (this is also done with preprocessor if you include x.h) you can instanciate object from that class. and CAN use its methods.

But When you use forward deleration you can only instantiate but you cannot use its methods until its complete decleration was seen by the compiler.

Cheers,
Mohammad Moghimi


All times are GMT -5. The time now is 09:36 AM.