defineproperty怎么读 define

MFC虽然没有未来,但是我觉得MFC的思想还是有必要研究研究的,在MFC中或者一些底层代码的编写中,宏是相当好用的,为什么呢?因为宏只是简单的替换,不进行类型转换,替换就意味着灵活,而C语言编程的灵魂就是灵活啊 。
但是在高级语言中,甚至C++中,是提倡用const的,不提倡用#define,因为#define有一定的副作用,玩不好就“加班”咯
这里我们讨论下#define的副作用 。
# include <stdio.h>
# include <stdlib.h>
# define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))
int main(void)
{
int a = 5;
int b = 0;
CALL_WITH_MAX(++a,b);
CALL_WITH_MAX(++a,b+20);
return 0;
}
分析:
上面的问题,不知道大家有没有看出来 。
对于CALL_WITH_MAX(++a,b);
1、a首先加1 ,变成了6
2、再和b进行比较,结果是a大,最后返回的结果是(++a),又被加1 ,最后的结果是7
对于CALL_WITH_MAX(++a,b+20);
1、a首先加1 ,变成了8
2、再和b进行比较,结果是a大,最后返回的结果是b,此时的b为20,那么a只加了一次,a的结果是8
# include <stdio.h>
# include <stdlib.h>
# define CALL_WITH_MAX(a,b) f((a) > (b) ? (a):(b))
void f(int x)
{
printf("compare result = %d\n",x);
}
int main(void)
{
int a = 5;
int b = 0;
【defineproperty怎么读 define】 printf("Initial value a = %d\n",a);
CALL_WITH_MAX(++a,b);
printf("excute the first a = %d\n\n",a);
printf("Initial value a = %d\n",a);
CALL_WITH_MAX(++a,b+20);
printf("excute the second a = %d\n\n",a);
return 0;
}
结果分析如下:


defineproperty怎么读 define

文章插图


好啦,今天就分享到这啦!喜欢此篇文章或觉得这篇文章对你有帮助的读者可以分享给身边的朋友们 。如果你是小白也可以私信回复“资料”领取大礼包一份,以及开发工具一份 。

    秒懂生活扩展阅读