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
    }
  • 相关阅读:
    B
    B
    G
    F
    E
    A
    C
    2017icpc 乌鲁木齐网络赛
    bzoj 2038 小Z的袜子(hose)(莫队算法)
    矩阵快速幂刷题系列
  • 原文地址:https://www.cnblogs.com/frankbadpot/p/1582784.html
Copyright © 2011-2022 走看看