#define CUSER_H
#include
class User
{
public:
User();
User(int a);
~User();
void setA(int a);
int getA();
protected:
private:
int a;
std::string str;
};
#endif
在CUser.h中声明了一个类,需要一个string的成员,就在这个文件的前面包含了
怎么检查是否会出现重复包含这个问题。
1.高级点儿的编译器都会对代码进行优化,你写一些必要的代码,才能增加体积……
2.我们要防止重复定义,所以需要使用#ifndef之类的宏,例如:
a.h中有
#include"b.h"
main.cpp中
#include"a.h"
#include"b.h"
那么在编译的时候b.h中的内容就会出现重复定义。
3.#if等宏是可以嵌套的。