宏观难题的范围 [英] Scope of macro puzzle

查看:65
本文介绍了宏观难题的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

void sum(){
#define SUM(a,b) a+b
}

int main(void){
   int a = 10; 
   int b = 20; 
   int c = SUM(a,b);
   int d = MUL(a,b);
   cout << c << endl;
   cout << d << endl;
   return 0;
}


void mul(){
#define MUL(a,b) a*b
}

该问题导致MUL宏出现错误.但是使用SUM宏可以很好地运行.为什么会这样?

The problem gives an error with MUL macro. But runs fine with SUM macro. Why is this happening?

推荐答案

这是因为在调用SUM宏之前先定义了它.

That's because you define the SUM macro before calling it.

它与在函数中定义的事实没有任何关系,即,在其定义以下的情况下,整个文件都可以访问它.

It does not have anything to do with the fact that it is defined in a function, i.e. it is accessible on the entire file, bellow its definition.

这篇关于宏观难题的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆