c++模板类的基础问题
有下列代码,可以在vs2013中编译通过
//template_test.h
#include <iostream>
using namespace std;
template<class T>
class mytest
{
public:
void method(T input);
void common();
};
template <>
class mytest<char>
{
public:
void method(char input);
void common();
};
template<class T>
void mytest<T>::method(T input)
{
cout << input << endl;
}
template<class T>
void mytest<T>::common()
{
cout << "common" << endl;
}
//template_test.cpp
#include "template_test.h"
void mytest<char>::method(char input)
{
cout << "char:" << input << endl;
}
void mytest<char>::common()
{
cout << "common" << endl;
}
//main.cpp
#include "template_test.h"
using namespace std;
int main()
{
mytest<char> test_char;
test_char.method('1');
test_char.common();
mytest<int> test_int;
test_int.method(1);
test_int.common();
system("pause");
return 0;
}
template<class T>
void mytest<T>::method(T input)
{
cout << input << endl;
}
template<class T>
void mytest::method(T input)
{
cout << input << endl;
}
template <>
class mytest<char>
{
public:
void method(char input);
void common();
};
//template_test.cpp
#include "template_test.h"
void mytest<char>::method(char input)
{
cout << "char:" << input << endl;
}
void mytest<char>::common()
{
cout << "common" << endl;
}
template<class T>
class mytest
{
public:
void method(T input);
void common();
};
template <>
class mytest<char>
{
public:
void method(char input);
void common();
};
template<>
void mytest<yourtype>::method(yourtype input){};