+ -
当前位置:首页 → 问答吧 → 字符指针做函数参数时候的一个小问题

字符指针做函数参数时候的一个小问题

时间: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;
}


求解!!!

作者: xiezefan   发布时间: 2011-12-22

ab是变量
"world"是常量

作者: haohaizijhz   发布时间: 2011-12-22

引用 1 楼 haohaizijhz 的回复:
ab是变量
"world"是常量

麻烦能解释下模板函数与普通函数在这题调用上区别的原因么?

作者: xiezefan   发布时间: 2011-12-22