zoukankan      html  css  js  c++  java
  • 成员函数模板

    真实指针支持隐式转换:1)Derived class指针可以隐式转换为Base class指针;2)"指向non-const对象"的指针可以转换成"指向const对象"的指针。

    智能指针:必须编写一个成员函数模板。因为我们无法写出所有的智能指针的构造函数,一旦Derived体系有新的补充就又要根据其他智能指针构造自己,实在太多了,根本不能写完。

    因为一个template可以被无限量具现化,以至于生成无限量函数,所以我们不写构造函数,而是写一个构造模板,称为成员函数模板。

    成员函数模板:

    根据自己的理解,我认为成员函数模板其实就是放在class里面的函数模板。

    如果声明member template用于"泛化copy构造"或"泛化assignment操作",你还是需要声明正常的copy构造函数和copy assignment操作符。

    什么叫泛化构造函数呢?

    template<class T>
    
    classshared_ptr{
    
    public:
    
    template<class Y>
    
        explicit shared_ptr(Y* p);
    
    template<class Y>
    
        shared_ptr(shared_ptr<Y> const& r); //泛化copy构造函数
    
    template<class Y>
    
        explicit shared_ptr(weak_ptr<Y> const& r);
    
    template<class Y>
    
        explicit shared_ptr(auto_ptr<Y> const& r);
    
    template<class Y>
    
        shared_ptr& operator=(shared_ptr<Y> const& r); //赋值
    
    template<class Y>
    
        shared_ptr& operator=(auto_ptr<Y> const& r);
    
    ……
    
    };

    当T和Y类型一样时,泛化copy构造函数就是普通的copy构造函数了。

  • 相关阅读:
    Android学习(二)
    密码与安全新技术专题之AI与密码
    Android学习(一)
    冲刺周六The Sixth Day(6.1)
    冲刺周日The Seventh Day(6.2)
    冲刺周五The Fifth Day(5.31)
    冲刺周四The Fourth Day(5.30)
    冲刺周三The Third Day(5.29)
    冲刺周二The Second Day(5.28)
    冲刺周一The First Day(5.27)
  • 原文地址:https://www.cnblogs.com/qiushuixiaozhanshi/p/5717474.html
Copyright © 2011-2022 走看看