为什么没有替换预处理程序指令?

2 c++ c-preprocessor

定义预处理程序指令的格式为:

#ifndef  SIZE
#define SIZE 10
int hello[SIZE];
#endif
Run Code Online (Sandbox Code Playgroud)

但是,当我查看以下代码时,预处理器指令没有替代品:

#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
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

Ker*_* SB 5

你可以说#define FOO,这意味着这#ifdef FOO是真的,但FOO没有任何替换文本.这对于像包含警卫这样的条件检查非常有用.

它对于特定于平台的扩展也很有用,在一般情况下,您希望它们为空:

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

API void foo();
Run Code Online (Sandbox Code Playgroud)