A template class that has a function for specific type

So you have a C++ template class, but you want to specifiy a member function for a particular type of data: 

 1 // A template class called Image: 
 2 template <class T>
 3 class Image {
 4 public:
 5   // ========================
 6   // CONSTRUCTOR & DESTRUCTOR
 7   Image() : width(0), height(0), data(NULL) {}
 8   Image(const Image &image) : data(NULL) { 
 9     copy_helper(image); }
10   const Image& operator=(const Image &image) { 
11     if (this != &image)
12       copy_helper(image);
13     return *this; }
14   ~Image() {
15     delete [] data; 
16   }
17   bool Load(const std::string &filename);
18 };
19 
20 // But you want to specify a function for a particular type:
21 template <> // notice this is an empty arrow bracket
22 bool Image<Offset>::Load(const std::string &filename) { // Offset is a class, you can specify Offset so you don't put a T in the arrow bracket
23  ... // sth here
24 }

 

转载于:https://www.cnblogs.com/RuiYan/p/4100901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值