#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
template <typename T>
class C
{
public:
C(int){
cout<<"C(int)"<<endl;
}
};
void Func(const C<double>& ) {
cout<<"Func(C<double> const& )"<<endl;
}
int main()
{
Func(23);
getch();
}
//
输出:
C(int)
Func(C<double> const& ) 这里的23 怎么会由一个int 型转换成一个类呢???
template <typename T>
class C
{
public:
C(int){
cout<<"C(int)"<<endl;
}
};
void Func(const C<double>& ) {
cout<<"Func(C<double> const& )"<<endl;
}
void Func(int) {
cout<<"Func(int)"<<endl;
}
int main()
{
Func(23);
getch();
}
// 输出:
// Func(int) 正确
//**********************************************************
template <typename T>
class C
{
public:
C(int){
cout<<"C(int)"<<endl;
}
};
void Func(const C<double>& ) {
cout<<"Func(C<double> const& )"<<endl;
}
void Func(int) {
cout<<"Func(int)"<<endl;
}
int main()
{
Func(23);
C<int> tmpC(22);
Func(tmpC); // error C2665: 'Func' : none of the 2 overloads could convert all the argument types
getch();
}
//下面我把有歧义的地方注释掉..发现不能转换..
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
template <typename T>
class C
{
public:
C(int){
cout<<"C(int)"<<endl;
}
};
void Func(int) {
cout<<"Func(int)"<<endl;
}
int main()
{
Func(23);
C<int> tmpC(22);
Func(tmpC);
getch();
}
//error C2664: 'Func' : cannot convert parameter 1 from 'C<T>' to 'int'
// 为什么类不能转换成int,而前面的int可以转换成一个类呢 ???
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
template <typename T>
class C
{
public:
C(int){
cout<<"C(int)"<<endl;
}
};
void Func(const C<double>& ) {
cout<<"Func(C<double> const& )"<<endl;
}
int main()
{
C<int> tmpC(22);
Func(tmpC);
getch();
}
//再次错误error C2664: 'Func' : cannot convert parameter 1 from 'C<T>' to 'const C<T> &'
// 不能由C<int> 转换成C<double>
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template <typename T>
class C
{
public:
C(int){
cout<<"C(int)"<<endl;
}
};
void Func(const C<double>& ) {
cout<<"Func(C<double> const& )"<<endl;
}
int main()
{
C<double> tmpC(22);
Func(tmpC);
getch();
}
//正确
//C(int)
//Func(C<double> const& )