About Scoped_ptr and Scoped_array

Source: Internet
Author: User
Scoped_ptr is a smart pointer in the boost library that is very similar to auto_ptr and can be used to automate the release of resources. The difference between scoped_ptr and auto_ptr is that auto_ptr can achieve the transfer of resources, but Scoped_ptr prohibits the transfer of resources, it allows an object to have a complete control of resources. The following code simulation implements SCOPED_PTR:
Template <class t>
class scopedptr
{public
:
    scopedptr (t* ptr)
        : _ptr (PTR)
    {}
    T & operator * ()//Returns the object you are pointing to
    {return
        *this;
    }
    t* operator-> () the function
    {return
        _ptr
    to invoke the smart pointer object using-> t* get ()//Gets the bare pointer
    {return
        _ptr;
    }
    ~autoptr ()
    {
        Destroy ();
    }
Protected:
    void Destroy ()
    {
        if (_ptr)
        {
            delete _ptr;
            _ptr = NULL;
        }
    }
Private:
    scopedptr (scopedptr<t>& ob);
    scopedptr<t>& operator = (scopedptr<t>& ob);
    t* _ptr;
};
Note: To prevent the transfer of a resource, both its copy constructors and the overloads of the replication operators are made private, thus preventing the transfer of resources
Disadvantage: Cannot convert permissions
The object lifecycle managed by Scoped_ptr is limited to an interval (between the "{}" of the pointer) and cannot reach the interval, meaning that the Scoped_ptr object cannot be the return value of the function (Auto_ptr can). Cannot share ownership
This feature makes the pointer easy to use, but it makes the pointer function weak, so that it can not be used in STL containers. cannot be used to manage array objects
The array object cannot be managed because Scoped_ptr deletes the managed object through delete. The Boost library gives the Scoped_array to manage the space that is applied to the new[]. Its implementation principle and scoped_ptr is the same. The following code is a mock implementation scoped_array:
Template <class t>
class Scopedarray
{public
:
    Scopedarray ()
    {}
    Scopedarray (t* ptr)
        : _ptr (PTR)
    {}
    t* get ()//Gets the bare pointer
    {return
        _ptr;
    }
    t& operator [] (int num) const
    {return
        _ptr[num];
    }
    ~scopedarray ()
    {
        Destroy ();
    }
Protected:
    void Destroy ()
    {
        if (_ptr)
        {
            delete[] _ptr;
            _ptr = NULL;
        }
    }
Private:
    Scopedarray (scopedarray<t>& ob);
    scopedarray<t>& operator = (scopedarray<t>& ob);
    t* _ptr;
};
The difference between Scoped_array and scoped_ptr:
The Scoped_array constructor must accept the new[]As a result, the Scoped_ptr constructor must accept the NewThe result Scoped_array must use delete[] to free the resource, scoped_ptr must use Delete to release the resource Scoped_array the overload of the two operators without * and->, which is a pointer to a space scoped_ Array provides an overload of [] that can be accessed as a normal array using subscript
Note: Although there is a scoped_array in the boost library, it is best to use the vector to complete the vector to achieve any type of dynamic array when the array needs to be dynamically allocated, and he has a rich member function to manipulate the data and is easy to maintain. But for Scoped_array, it is only the management of pointers, not dynamic growth or iterator support, relatively difficult to use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.