user707549
user707549

Reputation:

Why there is no replacement of the preprocessor directives?

The format of define preprocessor directives are:

#ifndef  SIZE
#define SIZE 10
int hello[SIZE];
#endif

But when I look at the following code, there is no replacement for the preprocessor directives:

#ifndef CREDIT_CARD_H                    // Avoid repeated expansion
#define CREDIT_CARD_H

#include <string>                        // Provides string
#include <iostream>                      // Provides ostream

class CreditCard
{
    public:
        CreditCard(const std::string& no,    // Constructor
                   const std::string& nm, int lim, double bal = 0);

        // Accessor functions
        std::string    getNumber()const    { return number; }
        std::string    getName() const     { return name; }
        double         getBalance() const  { return balance; }
        int            getLimit() const    { return limit; }

        bool chargeIt(double price);       // Make a charge
        void makePayment(double payment);  // Make a payment

    private:                               // Private member data
        std::string    number;             // Credit card number
        std::string name;                  // Card owner's name
        int            limit;              // Credit limit
        double        balance;             // Credit card balance
};

std::ostream& operator<<(std::ostream& out, const CreditCard& c);
#endif

What does this mean?

Upvotes: 3

Views: 914

Answers (4)

slf
slf

Reputation: 22767

I think what you are looking for is #pragma once

https://en.wikipedia.org/wiki/Pragma_once

Upvotes: 0

jcoder
jcoder

Reputation: 30035

It's an include guard to ensure that the file only gets included once.

#include "CreditCard.h"
#include "CreditCard.h"

The second time it's included CREDIT_CARD_H is already defines so it skips the whole definition.

Now you wouldn'd directly include a file twice like that, but it's common that one include file includes another and this stops any duplication definitions from happening.

The actual value is never used, only the fact that it's now defined

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

That type of directive pastes in the file depending on the condition:

#ifndef  SIZE

//whatever

#endif

Whatever is between the #ifndef and #endif is seen by the compiler only if SIZE wasn't defined.

The same happens for the second piece of code:

#ifndef CREDIT_CARD_H                   // avoid repeated expansion
#define CREDIT_CARD_H

//....

#endif

The class definition and includes will only be seen if CREDIT_CARD_H was not defined beforehand. This is known as an include guard.

You don't have to define a macro as something, you can just say #ifdef SIZE without actually "setting" size to anything, just defining it.

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 476940

You can say #define FOO, which means that #ifdef FOO will be true, but FOO doesn't have any replacement text. This is useful precisely for conditional checks like the include guards.

It can also be useful for platform-specific extensions, which you want to be empty in the general case:

#ifdef WIN32
#  define API __declspec(dllexport)
#else
#  define API
#endif

API void foo();

Upvotes: 5

Related Questions