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构造函数了。

  • 相关阅读:
    2019 SDN大作业
    个人作业——软件工程实践总结作业
    1.机器学习,从入门到放弃入门
    python25之进制转换
    python学习24之异常
    python学习23之标准库
    python学习22之函数式编程
    python学习21之高级特性
    python学习20之面向对象编程高级
    python学习19类5之多态与鸭子模型
  • 原文地址:https://www.cnblogs.com/qiushuixiaozhanshi/p/5717474.html
Copyright © 2011-2022 走看看