to cicala(知了) MSDN里面是这么说的 #pragma onceSpecifies that the file, in which the pragma resides, will be included (opened) only once by the compiler in a build. A common use for this pragma is the following://header.h #pragma once // Your C or C++ code would follow:
#define __OLDWORM_DEFINE_ABC_H__
#pragma once...#endif这是我的风格,其实#pragma once也是可以省略的,这是双保险写法。
#define
看一下MSDN~
还是有点糊涂.
比如说变量我可以用extern,但是class用不了吗?
那怎么在其他文件中用它啊?
extern是不一样的,它表明在link时,不同的obj代码文件可以共享一个变量。
但是同一个.h文件可以在不同的.cpp文件中都被包含。
#define '标示符' 声明内容#endif
我觉得用这种方式比较好
思路是:如果没有定义过该标示符,说明头文件没有被使用过
反之表示该头文件已经被使用过,因此不再进行重复声明
#pragma once这句的作用是什么?我不是很明白,能给我解释一下吗?
比如说我写个头文件,叫point.h
那么我就应该在point.h的开头写:
#ifndef point.h
#define point.h
#enddef
对吗?
如果在vc中最好别用这个,.net支持的很好。#ifndef point.h
#define point.h
#enddef
这样写好
#ifndef __POINT_H__
#define __POINT_H__
#endif
或者
#ifndef __POINT_H__
#define __POINT_H__ fjkfdjklfdsalkjfdslkjfdsalkjfdsalk
#endif
或者
#if!defined __POINT_H__
#define __POINT_H__
#endif
或者
#if!defined __POINT_H__
#define __POINT_H__ fjlfdsajlkfdsalkj;fdsalkj
#endif
MSDN里面是这么说的
#pragma onceSpecifies that the file, in which the pragma resides, will be included (opened) only once by the compiler in a build. A common use for this pragma is the following://header.h
#pragma once
// Your C or C++ code would follow:
#define __OLDWORM_DEFINE_ABC_H__
#pragma once...#endif