在Visual Studio2008中编写如下代码:

 

#include <iostream>
using namespace std;
 
int main()
{
#define MODI 10
    cout << MODI << endl;
#undef MODI
    cout << MODI + 1 << endl;
    return 0;
}

 

编译器会对 cout << MODI + 1 << endl;这行语句报错。

 

原因就是#undef起的作用:

当用完一个宏,比如MODI,不希望下面的代码再用到这个MODI,,那么就可以#undef它,那么下面如果再用到了MODI这个宏,编译器就会报错。

一种常用做法为:

#define MAX 50
#include "common.h"
#undef  MAX

这样就只有在common.h中才能使用宏MAX。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-11-28
  • 2021-12-26
  • 2021-10-24
  • 2021-05-29
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2022-02-15
  • 2021-06-26
  • 2021-06-06
  • 2022-12-23
相关资源
相似解决方案