[求教]为啥这个程序不走显式具体化模板,而是走隐式模板?
c++吧
全部回复
仅看楼主
level 6
RT代码如下
#include <iostream>
using namespace std;
template<typename T>
T max5(T arr[], int i);
template <> char* max5<char*>(char* arr[], int i);
int main()
{
/*int a[5] = { 1,9,3,4,5 };
cout << max5(a,5) << endl;
double b[8] = { 11.0,3.0,4.0,5.0,6.0,7.0,100.1,200.0 };
cout << max5(b,8) << endl;*/
char* ch[5] = { "abc","acbd","asdasd","asdasdasd","asdasdddad" };
cout << max5(ch[0], 5);
}
template<typename T>
T max5(T arr[], int i)
{
T temp;
int c = 0;
temp = arr[0];
while (c<i)
{
if (temp < arr[c])
temp = arr[c];
++c;
}
return temp;
}
template <> char* max5<char*>(char* arr[], int i)
{
char* temp = arr[0];
int c = 0;
while (c<i)
{
cout << strlen(temp);
if (strlen(temp) < strlen(arr[c]))
temp = arr[c];
++c;
}
return temp;
}
2017年02月16日 09点02分 1
level 6
[不高兴]
2017年02月16日 09点02分 2
level 15
所以你在说啥……
2017年02月16日 10点02分 3
level 15
char*不能用字符串字面值初始化。
2017年02月16日 10点02分 4
我知道了,cout << max5(ch[0], 5);这里传递的不是地址,cout << max5(&ch[0], 5);就对了
2017年02月16日 10点02分
这里为什么传的不是呢?定义是char* ch[5],是char型指针的数组,每一个数组元素里装的应该是一个指针,为什么还要加上取地址符呢[疑问]
2017年02月16日 11点02分
@记忆or成长 因为你的形参是char* arr[]
2017年02月16日 12点02分
回复
8826055
:[啊]所以呢?请说详细点
2017年02月16日 12点02分
1