What's the usage?

I

ikl

What's the usage of #ifdef and #endif in the code below?

#ifdef C_DEF
class C
{
public:
void Repair();
};

void C::Repair () {
....
}
#endif

Since I understand using like:

#ifdef C_DEF
class C
{
public:
void Repair();
};
#endif

void C::Repair () {
....
}


Thanks!
 
M

Mike Wahler

ikl said:
What's the usage of #ifdef and #endif in the code below?

#ifdef C_DEF

if the symbol 'C_DEF' is defined (with a #define directive),
then translate all the following code...
class C
{
public:
void Repair();
};

void C::Repair () {
...
}

.... until a

... directive is encountered.
Since I understand using like:

#ifdef C_DEF
class C
{
public:
void Repair();
};
#endif

This is the same as above, except it doesn't
include the definition of the member function
'Repair()'.
void C::Repair () {

A class 'C' must be visible at this point for
this to be valid.


Look up 'preprocessor directives' in your C++ book.

-Mike
 
J

JaSeong Ju

#ifdef and #endif are used for conditional compilation.
If C_DEF is defined, then compile in the class C and its
public member functions.

If C_DEF is not defined, then class C is
not included. Then of course you dont want to include
the
void C::Repair () {
....
}
member function, since its an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top