字符指针做函数参数时候的一个小问题
时间:2011-12-22
来源:互联网
char* Greater(char* x,char* y)
{
cout<<x<<endl;
return(strcmp(x,y)>0)?x:y;
}
第一以上函数:
char a[]="world",b[]="hello";
cout<<Greater("world","hello")<<endl;
cout<<Greater(a,b)<<endl;
以上两个 调用有什么区别;虽然输入都一样,但我很明确他们是有区别的;
因为在模板优先级问题中他体现出来了:
#include <iostream>
using namespace std;
char* Greater(char* x,char* y) //一般函数
{
cout<<"一般函数被调用"<<endl;
return(strcmp(x,y)>0)?x:y; //比较两个字符串的大小
}
template<class Ex>
Ex Greater(Ex x,Ex y);
template<> double Greater(double,double); //特化声明
int main()
{
char a[]="world",b[]="hello";
cout<<Greater(a,b)<<endl; //调用的是一般函数
cout<<Greater("world","hello")<<endl; //调用的是模板函数
return 0;
}
template<class Ex>
Ex Greater(Ex x,Ex y)
{
cout<<"模板函数被调用"<<endl;
return x>y?x:y;
}
求解!!!
{
cout<<x<<endl;
return(strcmp(x,y)>0)?x:y;
}
第一以上函数:
char a[]="world",b[]="hello";
cout<<Greater("world","hello")<<endl;
cout<<Greater(a,b)<<endl;
以上两个 调用有什么区别;虽然输入都一样,但我很明确他们是有区别的;
因为在模板优先级问题中他体现出来了:
#include <iostream>
using namespace std;
char* Greater(char* x,char* y) //一般函数
{
cout<<"一般函数被调用"<<endl;
return(strcmp(x,y)>0)?x:y; //比较两个字符串的大小
}
template<class Ex>
Ex Greater(Ex x,Ex y);
template<> double Greater(double,double); //特化声明
int main()
{
char a[]="world",b[]="hello";
cout<<Greater(a,b)<<endl; //调用的是一般函数
cout<<Greater("world","hello")<<endl; //调用的是模板函数
return 0;
}
template<class Ex>
Ex Greater(Ex x,Ex y)
{
cout<<"模板函数被调用"<<endl;
return x>y?x:y;
}
求解!!!
作者: xiezefan 发布时间: 2011-12-22
ab是变量
"world"是常量
"world"是常量
作者: haohaizijhz 发布时间: 2011-12-22
引用 1 楼 haohaizijhz 的回复:
ab是变量
"world"是常量
ab是变量
"world"是常量
麻烦能解释下模板函数与普通函数在这题调用上区别的原因么?
作者: xiezefan 发布时间: 2011-12-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28