求助!!!有关输出语句的问题

hackairM 2013-04-23 05:46:42
#include<iostream>
using namespace std;
template<typename AA>
AA max1(AA x,AA y)
{ cout<<"调用模板函数:";
return (x>y)?x:y;
}
int max1(int x,int y)
{ cout<<"调用非模板函数:";
return (x>y)?x:y;
}
int main()
{
int i=12,j=32,as[4]={1,2,3,4},tt;
double d=234,w=445,ad[4]={5,6,7,8},ee;
char c='d',l='a';
cout<<"the max between int is:"<<max1(i,j)<<endl;
cout<<"the max between double is :"<<max1(d,w)<<endl;
cout<<"the max between char is:"<<max1(c,l)<<endl;
system("pause");
return 0;
}


这段程序没有问题。
运行结果是
调用非模板函数:the max between int is:32
调用模板函数:the max between double is :445
调用模板函数:the max between char is:d


求助:为什么结果不是
the max between int is:调用非模板函数:32
the max between double is :调用模板函数:445
the max between char is:调用模板函数:d

...全文
111 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
akon_jx 2013-04-26
  • 打赏
  • 举报
回复
当一个函数作为函数参数时,参数总要先计算出来,外边的函数才能运行,cout是iostream类的对象,它有一个运算符函数operator<<
Code研究者 2013-04-25
  • 打赏
  • 举报
回复
引用 2 楼 u010335328 的回复:
引用 1 楼 howard58888 的回复:C/C++ code?1cout<<"the max between int is:"<<max1(i,j)<<endl; 1:不深入里面的话,其实就相当于fun(f()),将f()函数的结果传给fun函数 2:cout对像输出时,同理先调用max1(i,j),故先输出max1里的调用非模板函数或调用模板参数,再将结果返回……
hackairM 2013-04-24
  • 打赏
  • 举报
回复
引用 1 楼 howard58888 的回复:
C/C++ code?1cout<<"the max between int is:"<<max1(i,j)<<endl; 1:不深入里面的话,其实就相当于fun(f()),将f()函数的结果传给fun函数 2:cout对像输出时,同理先调用max1(i,j),故先输出max1里的调用非模板函数或调用模板参数,再将结果返回 传给cout对象输出
就是说max1(i,j)里的cout语句先输出了,然后返回的参数最后输出?
Code研究者 2013-04-23
  • 打赏
  • 举报
回复
cout<<"the max between int is:"<<max1(i,j)<<endl;
1:不深入里面的话,其实就相当于fun(f()),将f()函数的结果传给fun函数 2:cout对像输出时,同理先调用max1(i,j),故先输出max1里的调用非模板函数或调用模板参数,再将结果返回 传给cout对象输出

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧