码迷,mamicode.com
首页 > 其他好文 > 详细

A template class that has a function for specific type

时间:2014-11-16 01:51:43      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   sp   for   div   

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 }

 

A template class that has a function for specific type

标签:des   style   blog   io   color   ar   sp   for   div   

原文地址:http://www.cnblogs.com/RuiYan/p/4100901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!