Effective C++笔记 prefer consts,enums,and inlines to #defines static类成员,类内声明,类外定义(cpp) enum完成"in class初值设定" template inline函数 总结... typedef和#define的区别 1. 执行时间不同。 typedef在编译阶段进行处理,支持类型检查和调试,#define在预
目录 宏定义(#define) 概念 无参宏定义 举个例子 #define 用法的几点说明 带参数的宏定义 带参宏定义的说明 枚举类型(enum 关键字) 概念 过度 类型创建 类型定义,初始化 赋值 作为函数即返回值 typedef 存在的意义 用法 typedef 和 #define 的区别 宏定义(#define) 概念 #define 叫做宏定义命令,它也是 C ...
条款02:尽量以const,enum,inline替代#define @(EffectiveCpp) 新建一个Global.h文件,将本来通过#define定义的变量,用const或enum来定义;将通过#define定义的宏,用模板inline来代替。并将该Global.h包含到所有实现文件中,以模拟#define无视作用域的特点。 文章目录 条款02:尽量以`const...条款...
```cpp define MAX 1e6 int a[MAX]; ``` 此时会CE。因为1e6是一个double类型,数组大小只能用int,由于MAX是文本替换导致这里并不会转换类型。 这是可以在前面加上(int),或者使用const定义常量。 扩展阅读:Effective C++:以const、enum和inline来替换define ...
这里可以使用enum完成类似的功能 class People { private: enum { Number = 10 }; int phoneNumbers[Number]; ... } enum比较像#define而不像const。因为取const的地址是合法的,取一个enum的地址就不合法,取#define地址通常就不合法。所以可以通过enum来实现不让他人取得某个常量的地址。 下面...
(2). 使用宏 PRINT_SOURCE_INFO(),Debug/Release 方式编译输出结果大致相同,均是 MacroTest.cpp 的信息,只是 Debug 输出的 __FILE__ 是全路径,而 Release 输出的是相对路径: File: d:\source\macrotest\macrotest.cpp, Line: 14, Date: Aug 28 2011, Time: 07:42:30, Timestamp: Sun Aug 28 07:...
it is an opaque-enum-declaration (7.2), it is a template-parameter (14.1), it is a parameter-declaration (8.3.5) in a function declarator that is not the declarator of a function-definition,or it is a typedef declaration (7.1.3), ...
遇上一个关于enum,#define和const的问题,很郁闷! 最近一直在调试一个由纯C写的代码,由于种种原因,我不得已把工程改成了C++的。 因为是两个解决方案做了合并,很多名字冲突。现在只有宏的重定义,我把宏改成const编译总也不过,后来我又把宏使用枚举代替,还是编译不通过,提示我error C2143: syntax error : missi...
When the code is included again, the first ifndef fails, resulting in a blank file. That prevents double declaration of any identifiers such as types, enums and static variables. 2. 编译:编译器现在可以将所有预处理的源代码文件转换为二进制目标文件。 3. 链接:链接器将目标文件链接在一起。A ...
.cpp实现文件里设初始值 const double Student::score = 66.6; (很多编译器不支持在声明的时候设置初始值,只能将声明和设初值分开) 2、enum 如果遇到上述“不支持在声明的时候设置初始值”,就展现了enum的必要性。 eg: 代码语言:javascript 代码运行次数:0 ...