Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include vs import

Status
Not open for further replies.

eric91850

Programmer
Jul 29, 2007
12
0
0
US
Is that when ever I want to call any function or use any Class ojbect
/ structure, I will include that .h header file that contain that
Class object?

include <winsock.h>;

It's basically same as Java where I important the Class whenever I
want to use that Class -

import java.lang.String;
 
Yes but not exactly the same. Include files are used for all sorts of things, one of which is class definitions. They are also used for external function definitions, inline coding, templates, general declarations, macro definitions.

You may sometimes see include files being brought in more than once. eg
Code:
// include file elist.h
ETYPE(E8, "ATE")
ETYPE(E2, "TO")
ETYPE(E4, "FOUR")
ETYPE(E0, "NOT")
ETYPE(E1, "WAN")
ETYPE(E3, "TREE")

// Code file
#define ETYPE(e,v) e,
enum EType
{
#include "elist.h"
   ETypeMax
}
#undef ETYPE

#define ETYPE(e,v) #e "=" v,
const char* estr[] =
{
#include "elist.h"
   ""
};
#undef ETYPE
 
#include <file> just copies everything inside <file> and inserts it at the location where the #include line is.
So if you have a header like this:
Code:
class Test
{
public:
   Test();
...
};
and a .cpp file like this:
Code:
#include "Test.h"

Test::Test()
{
   // Initialize the Test object...
}
...
When you compile it, the pre-processor combines it into this:
Code:
class Test   // This used to be:  #include "Test.h"
{
public:
   Test();
...
};

Test::Test()
{
   // Initialize the Test object...
}
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top