zoukankan      html  css  js  c++  java
  • boost Smart Pointer

    Basic

    class Fruit
    {
    public:
        typedef boost::shared_ptr<Fruit> Pointer; // notice
        typedef boost::shared_ptr<Fruit const> ConstPointer; // notice

        Fruit(int weight)
            : m_Weight(weight)
        {}
    private:
        int m_Weight;
    };

    void Basic()
    {
        Fruit::Pointer ptr(new Fruit(1)); // notice
        Fruit * pRaw = ptr.get(); // notice
    };

    Shared from this

    class Fruit
        : public boost::enable_shared_from_this<Fruit> // notice
    {
    public:
        typedef boost::shared_ptr<Fruit> Pointer;
        typedef boost::shared_ptr<Fruit const> ConstPointer;

        static void PrintWeight(ConstPointer ptr)
        {
            cout << ptr->m_Weight << endl;
        }

        void PrintSelf() const
        {
            PrintWeight(shared_from_this()); // notice
        }
    };

    Customized Deletor

    void CustomizedDeletor()
    {
        boost::shared_ptr<FILE> pf(fopen("test.txt", "r"), fclose); // notice
        boost::shared_ptr<void> p(CoTaskMemAlloc(10), CoTaskMemFree); // notice
    }
  • 相关阅读:
    乐理学习
    hashtable
    vim配置
    SSH & Git
    Java实现单向链表反转
    Java实现二叉树遍历
    Mysql主从配置
    使用VirtualBox虚拟机搭建局域网
    Nginx配置try_files实践一
    Maven package打包webapp项目遇到的问题
  • 原文地址:https://www.cnblogs.com/frankbadpot/p/1582784.html
Copyright © 2011-2022 走看看