C++ 模板学习

1. 函数模板 及 类模板 定义实现使用

2. 函数模板重载
3. 类模板 定做 及部分定做 定义实现
4. LINUX UBANTU_G++不支援 显式实例化声名
 
----------------------------------
Time:2013-1-4

Developer: ubantu_g++

 
 
#include<iostream>
#include<string>
 
using namespace std;
 

//how to define a template function------------------------------------

template<typename T>
void print(T const & arg)
{

         cout<<"template function"<<endl\

             <<arg<<endl;
}

//how to overload a template function------------------------------------

template<typename T1,typename T2>

void print(T1 const& a, T2 const& b)

{

         cout<<"multi-args and overload function"<<endl\

             <<a<<endl\
             <<b<<endl;
}
 
 

//how to define a template class------------------------------------

template<typename T>
class obj
{
private:

         T content;

public:

         obj(T const& arg);

         void print(void);       

};
 
template<typename T>
obj<T>::obj(T const& arg)
{

         content = arg;

}
 
template<typename T>
void obj<T>::print(void)
{

         cout<<"template obj"<<endl\

             <<content<<endl;
}

//how to define a specialization class from a exist template class------------------------------------------

 
template<>
class obj<string>
{
private:

         string content;

public:

         obj(const string & str);

         void print(void);

};
 

obj<string>::obj(const string& str):content(str){}

 
void obj<string>::print(void)
{

         cout<<"specialization template"<<endl\

             <<content<<endl;
}
 

//mult_args template calss------------------------------------------

template<typename T1, typename T2>
class obj_
{
private:

         T1 cont0;

         T2 cont1;

public:

         obj_(const T1&, const T2&);

         void print(void);

};
 
template<typename T1, typename T2>

obj_<T1,T2>::obj_(const T1& a, const T2& b):cont0(a),cont1(b){}

 
template<typename T1, typename T2>
void obj_<T1,T2>::print(void)
{

         cout<<"mult_args template calss"<<endl\

             <<cont0<<endl\
             <<cont1<<endl;
}
 

//partial specialization template class------------------------------------------

template<typename T>
class obj_<T, string>
{
private:

         T cont0;

         string cont1;

public:

         obj_(const T&, const string&);

         void print(void);

};
 
template<typename T>

obj_<T, string>::obj_(const T & a, const string & b):cont0(a),cont1(b){}

 
template<typename T>
void obj_<T, string>::print(void)
{

         cout<<"partial specialization template class"<<endl\

             <<cont0<<endl\
             <<cont1<<endl;
}
 
//------------------------------------------
int main(void)
{

         int a = 100;

         double b =1.111;

         string str ("this is string");

        
        
        

        //test template function

         print(a);
         print(b);

         //test multi-types in template and overload it

         print(str,b);

         //test template class

         obj<int>object(100);
         object.print();

         //test mulit-types class

         obj_<string, int>object_m(str, a);

         object_m.print();

        //test specialization template class

         obj<string>ObjStr(str);
         ObjStr.print();

         //test partial specialization template class

         obj_<int ,string>object_(a,str);

         object_.print();
        

         //test instantial delay

         //template obj<double>;显式实例化声名 无法通过编译

 

         return 0;

}

你可能感兴趣的:(C++,模板)