c++ - 为什么没有替换预处理器指令?

标签 c++ c-preprocessor

定义预处理器指令的格式是:

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

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

#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

这是什么意思?

最佳答案

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

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

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

API void foo();

关于c++ - 为什么没有替换预处理器指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034102/

相关文章:

由于 #ifdef,ctags 在带有不平衡大括号的源文件上阻塞

c - #define 字符串与数字定义

c++ - 用于替换其参数中的标识符的宏

c++ - 指向类成员函数错误的函数指针

c++ - 为什么 cin >> string 不能与 Visual C++ 2010 一起使用?

c++ - 如果在运行时可用,是否自动使用 AVX/SSE?

c - 为什么要在C中定义同名同内容的宏?

c++ - 如果没有 'auto' ,是否有我无法使用的功能(特别是结构化绑定(bind))?

c++ - 我如何在没有 Stod() 的情况下重写

macros - 比较 C 宏中的字符串(用于 MODULE_LICENSE)