我如何搜索这个问题(范围内的宏与内联函数)
How do I search about this problem(Macro vs Inline function at scope)
我对范围有一些疑问。
我认为宏的作用类似于复制和粘贴,内联函数与此类似但不相同。
但是,我不知道为什么这段代码与我的想法不同。
我不知道要搜索什么关键字,请告诉我一些要搜索的关键字。
感谢阅读。
'''
inline int add(int a, int b)
{
return a + b;
}
int main()
{
int num1;
num1 = add(10, 20);
printf("%d\n", num1);
return 0;
} // is same as below
'''
int main()
{
int num1;
num1 = int add(int a=10, int b=20)
{
return a + b;
};
printf("%d\n", num1);
return 0;
}
'''
#define xtest() cout<<x<<endl
int x=0;
inline void test(){
cout<<x<<endl;
}
int main(void){
int x=10;
test();
cout<<x<<endl;
xtest();
{
int x = 20;
test();
cout<<x<<endl;
xtest();
}
return 0;
}
我理解 xtest()
和 {cout << x << endl}
的行为相同并且结果是正确的,但为什么 test()
的行为不同?
标记为 inline
的函数仅在一个方面不同于非 inline
函数:它们可以在多个翻译单元(= 源文件)中定义,而不会导致违反单一定义规则(=链接器错误)。那是 所有 。在所有其他方面,它们的行为与任何其他功能一样。它们与宏完全不同。
我对范围有一些疑问。
我认为宏的作用类似于复制和粘贴,内联函数与此类似但不相同。
但是,我不知道为什么这段代码与我的想法不同。
我不知道要搜索什么关键字,请告诉我一些要搜索的关键字。
感谢阅读。
'''
inline int add(int a, int b)
{
return a + b;
}
int main()
{
int num1;
num1 = add(10, 20);
printf("%d\n", num1);
return 0;
} // is same as below
'''
int main()
{
int num1;
num1 = int add(int a=10, int b=20)
{
return a + b;
};
printf("%d\n", num1);
return 0;
}
'''
#define xtest() cout<<x<<endl
int x=0;
inline void test(){
cout<<x<<endl;
}
int main(void){
int x=10;
test();
cout<<x<<endl;
xtest();
{
int x = 20;
test();
cout<<x<<endl;
xtest();
}
return 0;
}
我理解 xtest()
和 {cout << x << endl}
的行为相同并且结果是正确的,但为什么 test()
的行为不同?
标记为 inline
的函数仅在一个方面不同于非 inline
函数:它们可以在多个翻译单元(= 源文件)中定义,而不会导致违反单一定义规则(=链接器错误)。那是 所有 。在所有其他方面,它们的行为与任何其他功能一样。它们与宏完全不同。