首页 > 代码库 > A template class that has a function for specific type
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 bracket22 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 bracket23 ... // sth here24 }
A template class that has a function for specific type
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。