功能参数处理 [英] function parameter treatment

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

问题描述

我在vs2013中测试了这段代码



#include< iostream>

void test(int a,int b,int c )

{

std :: cout<< \ n<< a<< - << b<< - << c;

}

void main()

{

int a = 1;

test(a ++,++ a,a);

a = 1;

test(a ++,a ++, a ++);

a = 1;

test(++ a,++ a,++ a);

a = 1;

test(a ++,++ a,a ++);

}

和out put是

2-3- 3

3-2-1

4-4-4

3-4-1

然后我在bc中测试了这段代码

#include< iostream.h>

void test(int a,int b,int c)

{

cout<< \ n<< a<< - << b<< - << c;

}

void main()

{

int a = 1;

test(a ++,++ a,a);

a = 1;

test(a ++,a ++, a ++);

a = 1;

test(++ a,++ a,++ a);

a = 1;

test(a ++,++ a,a ++);

}

然后输出是

2-2- 1

3-2-1

4-3-2

3-3-1

如果我们认为参数是从右手发送到函数bc中第二个代码的输出是不可靠的但我无法理解第一个代码的输出。

谁可以帮助我?

I tested this code in vs2013

#include <iostream>
void test(int a, int b,int c)
{
std::cout << "\n" << a << "-" << b<<"-"<<c;
}
void main()
{
int a = 1;
test(a++, ++a, a);
a = 1;
test(a++, a++, a++);
a = 1;
test(++a, ++a, ++a);
a = 1;
test(a++, ++a, a++);
}
and out put was
2-3-3
3-2-1
4-4-4
3-4-1
then I tested this code in bc
#include <iostream.h>
void test(int a, int b,int c)
{
cout << "\n" << a << "-" << b<<"-"<<c;
}
void main()
{
int a = 1;
test(a++, ++a, a);
a = 1;
test(a++, a++, a++);
a = 1;
test(++a, ++a, ++a);
a = 1;
test(a++, ++a, a++);
}
then the output was
2-2-1
3-2-1
4-3-2
3-3-1
if we think that arguments are sent from right hand to function the output of second code in bc is undestandable but I can't understand the output of first code.
who can help me?

推荐答案

多个函数参数的评估顺序是未定义的!根据你的测试,参数是从右到左评估的,但你不能指望它始终如此:不同的编译器,甚至同一编译器的不同版本可能表现不同,从而产生不同的结果!



不要依赖于函数参数的特定评估顺序!



有说,我只能同意上面的pwassers评论。



PS: http://stackoverflow.com/questions/2934904/order-of-evaluation-in-c-function-parameters [ ^ ]
The order of evaluation in the case of multiple function arguments is undefined! According to your tests, arguments were evaluated right to left, but you cannot count on that to be the case at all times: different compilers, or even different versions of the same compiler may behave differently, and thus produce different results!

Do not rely on a specific order of evaluation of function arguments!

Having said, that, I can only agree to pwassers comment above.

P.S.: http://stackoverflow.com/questions/2934904/order-of-evaluation-in-c-function-parameters[^]


这篇关于功能参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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