#define的错误使用
来源:互联网 发布:ubuntu挂起 编辑:程序博客网 时间:2024/06/05 16:24
在MFC当中我们比较容易看到用#define定义的宏,但是红定义却比较容易出错。
以下是修改《effective C++》中的
#define CALL_WITH_MAX(a,b) (a)>(b)?(a):(b);
当我们计算
int a=5;
int b=0;
int c=CALL_WITH_MAX(++a,b);
时候 我们会对a加了两次这是因为#define 是在预编译时候就进行替换。所以在编译的时候,其实处理的是
int c=(++a)>(b)?(++a):(b);
而当a>b的时候,a加了两次,小于等于时候加了一次。
#include<iostream>#define CALL_WITH_MAX(a,b) (a)>(b)?(a):(b);using namespace std;int main(){int a=5;int b=0;int c=0;c=CALL_WITH_MAX(++a,b);cout<<c<<endl;return 0;}
所以我们尽量不用而是采用c++中的template
#include<iostream>using namespace std;template<typename T>inline void callwithmax(const T&a,const T&b){cout<<(a>b?a:b);}int main(){int a=5;int b=0;int c=0;callwithmax(++a,b);return 0;}
0 0
- #define的错误使用
- #define的使用总结
- #define的使用注意
- define的使用技巧
- define的使用技巧
- define的使用
- #define 中 #/##的使用
- #define 的使用
- #define的使用
- #define的使用
- 诡异的C++ #define编译错误
- Multiple dex files define的错误原因
- C++中define的使用
- 宏定义define的使用
- #define宏定义的使用
- define命令的另类使用
- #define 宏的另一种使用
- 宏定义#define的使用
- 数据结构试题解答思路
- 关于搭建Apache和Tomcat集群遇到的问题
- vi下对齐代码的操作
- 贝叶斯定理推广
- SQL数据库,如何把一张表从一个数据库中插入到另外一个数据库
- #define的错误使用
- Apache的Order Allow,Deny 详解
- fatal error LNK1104: 无法打开文件“***.lib“ 解决方法
- SQL联合查询
- linux 与winscp /SecureCRT 链接关闭防火墙以及selinux问题
- c++中的friend关键字
- 应用之星推出“图文app”制作工具,并附上教程
- man命令的使用方法
- nodejs 微信开发相关,抓取相关