首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2013-06-26 
求助!!!有关输出语句的问题#includeiostreamusing namespace stdtemplatetypename AAAA max1(AA x,AA

求助!!!有关输出语句的问题
#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

C++ ??cout
[解决办法]

引用:
引用: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是iostream类的对象,它有一个运算符函数operator<<

热点排行