Effective C++ 45. Use member function templates to accept "all compatible types."
来源:互联网 发布:知乎手机上网页版 编辑:程序博客网 时间:2024/05/29 06:36
I
class Top { };class Middle: public Top{ };class Bottom: public Middle { };Top *pt1 = new Middle;Top *pt2 = new Bottom;const Top *pt2 = pt1;
tmplate<typename T>class SmartPtr {public:explicit SmartPtr(T* realPtr);};SmartPtr<Top> pt1 = SmartPtr<Middle>(new Middle);
There is no inherent relationship among different instantiations of the same template, so compilers view SmartPtr<Middlet> and SmartPtr<Top> as completely different classes.
template<typename T>class SmartPtr {public: template<typename U> SmartPtr(const SmartPtr<U>& other);};
we want to be able to create a SmartPtr<Top> from a SmartPtr<Bottom>, but we do not want to be able to create a SmartPtr<Bottom> from a SmartPtr<Top>, as that’s contray to the meaning of public inheritance.
I.I
template<typename T>class SmartPtr{public: template<typename U> SmartPtr(const SmartPtr<U>& other): heldPtr(other.get()) { } T* get() const { return heldPtr; };private: T* heldPtr;};
heldPtr(other.get()) this will compile only if there is an impilcit conversion from a U* pointer to a T* pointer, and that is precisely what we want.
II.
Declaring a generalized copy constrctor (a member template) in a class doesn’t keeep compilers from generating their own copy constructor, so if you want to control all aspects of copy construction, you must declare both a generalized copy constructor as well as the “normal” copy constructor.
template<typename T> clas shared_ptr {public: // copy constructor shared_ptr(shared_ptr const& r); // generalized copy constructor template<typename Y> shared_ptr(shared_ptr<Y> const& r); // copy assignment shared_ptr& operator=(shared_ptr const& r); // generalized copy assignment template<class Y> shared_ptr& operator = (shared_ptr<Y> const& r);};
- Effective C++ 45. Use member function templates to accept "all compatible types."
- 用 member function templates 接受 "all compatible types"
- [翻译] Effective C++, 3rd Edition, Item 45: 用 member function templates(成员函数模板) 接受 "all compatible types"(“所有兼容类型”)
- How to use (data_member | member function) pointers in class
- Question 20: Which allocator member function do standard containers use to acquire storage for their elements in C++?
- effective c++ 条款23 perfer nonmember nonfreind function to member function
- effective java-use function objects to represent strategies
- Const Member Function(C++)
- Error c3876: function call missing argument list; use '' to create a pointer to member
- 【c++】cannot declare member function *** to have static linkage [-fpermissive]
- Effective C++ 24. Declare non-member functions when type convesions should apply to all parameters
- C++templates简单基础--Function Templates
- C++ use Templates(function && class)
- experiment : use class member function pointer
- Friend class or function and nested types in class templates
- lua study -- how to call lua function use C language
- How to Be an Effective Team Member
- How To Be an Effective Team Member
- js 禁止用户复制页面内容和修改html
- 基于redux的前端框架dva入门教程
- 通过sql语句恢复数据库
- 如何将重采样的数据扔给编码器(AVAudioFifo)
- mysql查询今天、昨天、上周
- Effective C++ 45. Use member function templates to accept "all compatible types."
- Java第十二章练习习题
- ros学习笔记(ova1)CMake语句解析
- 可能碰到的iOS笔试面试题(10)--UI
- readline中的键盘码^[[A,^[[B,^[[D,^[[C (ANSI控制码)
- learn opencv-如何在OpenCV中选择边界框(ROI)?
- ubuntu配置sublime
- 直播 | Kubernetes调度
- MatlabR2012a显示使用过期的注册文件破解