C ++ constructor, destructor, copy constructor, assign value constructor, a memory error example!

Source: Internet
Author: User

 

// Pointer_control2.cpp: defines the entry point of the console application.
//

# Include "stdafx. H"

Class u_ptr {
Friend class hasptr;
Int * IP address;
Size_t use;
U_ptr (int * P): IP (P), use (1 ){}
~ U_ptr () {Delete IP ;}
};

/* Smart pointer class: takes ownership of the dynamically allocated
* Object to which it is bound
* User code must dynamically allocate an object to initialize a hasptr
* And must not delete that object; the hasptr class will delete it
*/
Class hasptr {
Public:
// Hasptr owns the pointer; pmust have been dynamically allocated
Hasptr (int * P, int I): PTR (New u_ptr (p), Val (I ){}

// Copy members and increment the use count
Hasptr (const hasptr & orig): PTR (orig. PTR), Val (orig. Val) {++ PTR-> use ;}
Hasptr & operator = (const hasptr &);

// If use count goes to zero, delete the u_ptr object
~ Hasptr () {If (-- PTR-> Use = 0) delete PTR ;}

// Copy control and constructors as before
// Accessors must change to fetch value from u_ptr object
Int * get_ptr () const {return PTR-> IP ;}
Int get_int () const {return val ;}

// Change the appropriate data member
Void set_ptr (int * P) {PTR-> IP = P ;}
Void set_int (int I) {val = I ;}

// Return or change the value pointed to, so OK for const objects
// Note: * PTR-> ip is equivalent to * (PTR-> ip)
Int get_ptr_val () const {return * PTR-> IP ;}
Void set_ptr_val (int I) {* PTR-> IP = I ;}

PRIVATE:
U_ptr * PTR; // points to use-counted u_ptr class
Int val;
};

Hasptr & hasptr: Operator = (const hasptr & RHs)
{
++ RHS. PTR-> use; // increment use count on RHS first
If (-- PTR-> Use = 0)
Delete PTR; // If use count goes to 0 on this object, delete it
PTR = RHS. PTR; // copy the u_ptr object
Val = RHS. Val; // copy the int Member
Return * this;
}

 

Int _ tmain (INT argc, _ tchar * argv [])
{
Int OBJ = 0;
Int * P = & OBJ;
Hasptr ptr1 (p, 42); // int * Member points to OBJ, Val is 42
Hasptr ptr2 (ptr1); // int * Member points to OBJ, Val is 42
Hasptr ptr3 = ptr1; // =

Ptr1.set _ int (0); // Changes Val member only in ptr1
Printf ("% d \ n", ptr2.get _ int (); // returns 42
Printf ("% d \ n", ptr3.get _ int (); // pth3 returns 42

Ptr1.get _ int (); // returns 0
Printf ("% d \ n", ptr1.get _ int (); // returns 0

Ptr1.set _ ptr_val (45); // Sets object to which both ptr1 and ptr2 point
 
Printf ("% d \ n", ptr2.get _ ptr_val (); // returns 42
Return 0;
}

 

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.