"Turn" C + + Face Question (iv)--the principle and realization of intelligent pointer

Source: Internet
Author: User
Tags throw exception

C + + face Question (iv)--the principle and realization of intelligent pointer tanglu2004 http://blog.csdn.net/worldwindjp/
 

C + + face questions (i), (ii) and (c) are all done, congratulations on your coming here, this is basically the C + + interview problem of the last wave.

1, do you know smart pointers? The principle of smart pointers.
2, commonly used smart pointers.
3, the implementation of smart pointers.

1 Answer: The smart pointer is a class that passes in a normal pointer in the constructor of the class, releasing the incoming pointer in the destructor. The classes of smart pointers are objects on the stack, so when the function (or program) ends, it is automatically released.

2, the most commonly used smart pointers:

1) Std::auto_ptr, there are many problems. Replication (copy constructor) and assignment (operator =) are not supported, but errors are not prompted when copying or assigning values. cannot be placed in a container because it cannot be copied.

2) c++11 introduced Unique_ptr, also does not support copying and assignment, but better than auto_ptr, direct assignment will compile error. If you really want to assign a value, you need to use: Std::move.

For example:

std::unique_ptr<int> P1 (new int (5));
std::unique_ptr<int> P2 = p1; Compile error
Std::unique_ptr<int> p3 = Std::move (p1); Transfer ownership, now that memory belongs to P3, P1 becomes invalid pointer.

3) c++11 or boost shared_ptr, a smart pointer based on reference counting. It can be arbitrarily assigned until the memory reference count is 0 and the memory is freed.

4) c++11 or boost weak_ptr, weak reference. One problem with reference counting is that they reference each other to form a ring, so that the memory that the two pointers point to cannot be freed. You need to manually break the circular reference or use weak_ptr. As the name implies, Weak_ptr is a weak reference, only a reference, not a count. If a piece of memory is referenced by both shared_ptr and weak_ptr, the memory is freed, regardless of whether or not weak_ptr references the memory after all shared_ptr have been refactored. So weak_ptr does not guarantee that the memory it points to must be valid, and you need to check if weak_ptr is a null pointer before using it.

3, the implementation of smart pointers

The following is an implementation of a smart pointer based on a reference count, which requires implementation of construction, destruction, copy construction, = operator overloading, overloading-* and > Operators.

1Template <typename t>2 classSmartpointer {3  Public:4     //constructor Function5Smartpointer (t* p=0): _ptr (P), _reference_count (Newsize_t) {6         if(P)7*_reference_count =1; 8         Else9*_reference_count =0; Ten     } One     //copy Constructor ASmartpointer (Constsmartpointer&src) { -         if( This!=&src) { -_ptr =src._ptr; the_reference_count =Src._reference_count; -(*_reference_count) + +; -         } -     } +     //Overloaded assignment operator -smartpointer&operator=(Constsmartpointer&src) { +         if(_ptr==src._ptr) { A             return* This; at         } - Releasecount (); -_ptr =src._ptr; -_reference_count =Src._reference_count; -(*_reference_count) + +; -         return* This; in     } -  to     //Overloaded Operators +t&operator*() { -         if(PTR) { the             return*_ptr; *         } $         //Throw ExceptionPanax Notoginseng     } -     //Overloaded Operators thet*operator-() { +         if(PTR) { A             return_ptr; the         } +         //Throw Exception -     } $     // Destructors $~Smartpointer () { -         if(--(*_reference_count) = =0) { - Delete _ptr; the Delete _reference_count; -         }Wuyi     } the Private: -T_ptr; Wusize_t *_reference_count; -         voidReleasecount () { About         if(_ptr) { $(*_reference_count)--; -                 if((*_reference_count) = =0) { - Delete _ptr; - Delete _reference_count; A                 } +         } the         } - }; $  the intMain () the { thesmartpointer<Char> CP1 (New Char('a')); thesmartpointer<Char>CP2 (CP1); -smartpointer<Char>CP3; inCP3 =CP2; theCP3 =CP1; theCP3 =CP3; Aboutsmartpointer<Char> CP4 (New Char('b')); theCP3 =CP4; the}

"Turn" C + + Face Question (iv)--the principle and realization of intelligent pointer

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.