Boost Library at work (6) Scope smart pointer scoped_ptr Five

Source: Internet
Author: User
Tags constant constructor

By learning from the previous smart pointers, you should be aware of the use and limitations of std::auto_ptr, thus triggering the boost library to provide more smart pointers for these situations, where scoped_ptr is provided for std::auto_ptr. For developers, the more accurate the code is, the better the compiler can find as many errors as possible. For example, if you want a variable to not change its value, you need a constant qualifier, so you can eliminate any careless operation of the variable. In normal pointers, const is often used to qualify pointers that cannot be pointed to other pointers. What if you need a pointer that does not point to another pointer and can automatically delete the smart pointer? Can you use the following scenario to resolve it? The code is as follows:

const std::auto_ptr< int > pTemp1 (new int);

This way the pointer pTemp1 a constant, cannot point to another pointer, and is also a smart pointer. This solution can be solved, but the solution is not very graceful, such as the need to write a const, resulting in long input, and no flexibility, such as to replace the smart pointer pointing to the situation, this method is not.

Is there a better solution? Of course there is the use of scoped_ptr, which is a scope pointer and cannot be copied, assigned, but can be re-directed to the new pointer by the function reset. The main difference between scoped_ptr and std::auto_ptr is whether the saved pointer can transfer control. Std::auto_ptr is to support the transfer of control, while SCOPED_PTR does not support it. When you write code, you do not want someone to take your pointer, you can use Scoped_ptr to make sure that only one pointer to the allocated memory, but also do not let other pointers to overwrite it. When you declare a pointer in a class and initialize the table before the constructor allocates memory, but throws an exception in the constructor, which leads to a memory leak, it needs to be resolved using scoped_ptr, as in the following example:

class Cscopedtest
{public
:
	cscopedtest ()
		: m_ptest (new int)//If smart pointers are not used here, there may be a memory leak.
	{
		//This may be thrown abnormally.
	}
Private:
	boost::scoped_ptr< int > m_ptest;
};

Examples of specific uses of SCOPED_PTR are as follows:

Using Scoped_ptr//
software Developer: Cai Junsheng  2013-02-03
void testscopedptr (void)
{
	//define variable
	boost:: scoped_ptr< int > pVal (new int);
	if (PVal)
	{
		*pval = $;
	}	

	std::cout<< *pval << Std::endl;

	The following line of assignment compiles an error.
	//boost::scoped_ptr< int > ptemp = pVal;

	boost::scoped_ptr< int > ptemp;
	The following line determines the compilation error
	//if (ptemp = = pVal)
	//{
	//}
	
	//This line is similar to boost::scoped_ptr
	const STD::AUTO_PTR < int > pTemp1 (new int);
	
	Resets the pointer.
	pval.reset (new int);

}


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.