Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-09-2004, 01:08 AM   #1
LQ Newbie
Registered: Sep 2004
Posts: 3

Rep: Reputation: 0
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?

Last edited by scoTtkIm; 09-09-2004 at 01:09 AM.
Old 09-09-2004, 04:23 AM   #2
Registered: Aug 2004
Distribution: Mandrake 10.2(Mandriva), SuSE 9.3, Slackware 9.1, Xandros 3.1, Knoppix 3.9, FreeBSD 5.3, RedHat9
Posts: 122

Rep: Reputation: 15
Then main difference between forwarding declearion and including or putting class decleration.

This is a forwarding declearion
class X;
This is a class decleration
class X
And this is a include !
#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.

Mohammad Moghimi


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between function declaration: func() and func(void) koyi Programming 4 11-19-2005 10:19 AM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
How to write Makefile in 2.6 for including header files in another dir ? ashbalu Programming 0 10-19-2004 03:45 PM
How do I custom write a library and use it in the header declaration ? Linh Programming 4 05-26-2004 03:38 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration