看到有网友发贴(参考下面链接)
http://student.youkuaiyun.com/space.php?uid=8212&do=thread&id=2868
出了一道题。此题还引来了一些“想骂人”的冲动。
估且不猜测出题人的真实意图,在实际项目中如此写代码的确有“招骂”之嫌。实际上,这道题的背后是希望学习者能了解C++的函数调用规则。
熟悉Windows的开发者应该了解函数声明和定义时前面加上下面这些修饰
WINAPI, PASCAL,CALLBACK,以及__cdecl, __stdcall, __fastcall
资料上能查到的信息本文就不再复制了,读者可以在msdn中寻找详细描述,超懒的读者可以直接访问下面的链接
http://msdn.microsoft.com/en-us/library/k2b2ssfy(VS.71).aspx
如果说看不懂那我也没有办法了。 我的观点是,以E文代差不看资料的学习者,趁早改行吧。
下面给出“探索C++函数默认的调用方式”,希望你能输入到计算机中编译运行看到结果,并能从结果中分析出正确结论。
源代码如下
- #include <iostream>
- using namespace std;
- int foo(int x, int y)
- {
- cout << " x = " << x << "/t y = " << y << endl;
- return 0;
- }
- int bar(int x)
- {
- cout << "bar ( " << x <<" )" << endl;
- return x;
- }
- int main()
- {
- cout << " test start " << endl;
- int a = 3;
- int b = foo(a, a++);
- int c = foo(bar(1),bar(2));
- return 0;
- }
总有一些人是只想要结果,不想要过程,下面我把我的输入结果贴出来供这样的人直接查看。
test start
x = 4 y = 3
bar ( 2 )
bar ( 1 )
x = 1 y = 2