(Original) C ++ smart pointer-Releaser (in Linux, G ++)

Source: Internet
Author: User

My records:

1. If the Releaser is specifiedDirectCall the destructor. Because the Releaser is one. If this parameter is not specified, the Destructor is the Releaser.

2. STL smart pointers support less built-in internal types than Class: shared_ptr <char> S (New char [10]); memset (S, 0x00, 10 );//Error! Only memset (S. Get.

3. The Releaser cannot be specified for auto_ptr.

# Include "iostream" # include <tr1/memory> using namespace STD; Using STD: tr1: shared_ptr; Using STD: auto_ptr; Class C {public: C () {cout <"C ()" <Endl; Buf = new char [10];} ~ C () {cout <"~ C () "<Endl; If (BUF) {cout <"~ C () Delete [] Buf. "<Endl; Delete [] Buf ;}} static void myrelease (C * PC) {cout <" myrelease () "<Endl; cout <" myrelease () delete [] Buf. "<Endl; Delete [] PC-> Buf; PC-> Buf = NULL; cout <" myrelease () delete C: "; Delete PC; // call ~ C ().} PRIVATE: char * Buf;}; int main () {cout <"STD: tr1: shared_ptr <C >:" <Endl; {C * A = new C; shared_ptr <C> sharec (A, C: myrelease);} cout <"<-shared_ptr test ended. \ n "<Endl; cout <" STD: tr1: shared_ptr <C> copy test "<Endl; {shared_ptr <C> sharec (New C, C:: myrelease); shared_ptr <C> sharec2 = sharec;} cout <"<-shared_ptr copy test ended. \ n "<Endl; cout <" STD: auto_ptr <C>: "<Endl; C * B = new C; auto_ptr <C> AutoC (B ); cout <"Return 0;" <Endl; return 0 ;}


$ g ++ main. cpp

Related Article

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.