#ifndef BUFFER_H_3234242
#define BUFFER_H_3234242
#include <iostream>
template<class T,int SIZE>
class Buffer {
private:
T v[SIZE];
int sz;
int dsz;
public:
Buffer():sz(SIZE) {}
const int size() const;
};
template<class T,int SIZE>
const int Buffer<T,SIZE>::size() const
{
return sz;
}
/*
类模板特化
*/
template<>
const int Buffer<int,1>::size() const
{
return sz*2;
#define BUFFER_H_3234242
#include <iostream>
template<class T,int SIZE>
class Buffer {
private:
T v[SIZE];
int sz;
int dsz;
public:
Buffer():sz(SIZE) {}
const int size() const;
};
template<class T,int SIZE>
const int Buffer<T,SIZE>::size() const
{
return sz;
}
/*
类模板特化
*/
template<>
const int Buffer<int,1>::size() const
{
return sz*2;
}
#endif
如上面所示,size()函数如果没有特化,那么返回sz
特化以后,返回sz*2
这样,对于size()函数,产生了2种不同的用途,如果需要产生更多的用途,可以特化更多的版本