定义预处理器指令的格式是:
#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/