Class pointer member management

Source: Internet
Author: User
 

The pointer member in the previously tangled noname is discussed in Chapter 13.5.

Class pointer member management: danglingptr. some operations and results after CPP deletes an IP Address: delete IP; STD: cout <"after delete: * "<IP <" is "<* IP <STD: Endl; // It indicates that delete is valid. The object is cleared, but the Pointer Points to the original position. PTR. set_ptr_val (220); // although it is a disaster to forcibly assign a value to the original int object, STD: cout <PTR. get_ptr_val () <STD: Endl; // However, a common int and disaster cannot be displayed. It is also common to use 13.21 questions such as a string, write an destructor to delete pointer members.

The old man in the book does not understand this problem. The noname in the front should be an incorrect expression.

If there is a delete * PTR In the destructor,

Int I = 42; hasptr p1 (& I, 42 );

A memory error occurs directly. At least Exit Main will analyze the structure once, and the error will occur.

About Smart Pointers-in fact, we are trying to add a counting function to ensure that all objects are deleted only when they are revoked.

Use a "private class" to complete the counting function

TIPS: All u_ptr members are private, and user Meta is specified to make u_ptr a private class of hasptr.

Not complete -- the assignment operator is missing ....

U_ptr.cpp

// What happens when a destructor deletes a pointer member? // It is not a good habit to read a sentence from a book. If you have more statements, you must convert them to the normal mode. // what is the point? Smart pointers only ensure that the same object cannot delete two pointers, but there will be multiple u_ptr objects without copying, so that each u_ptr object's delete IP address will not conflict with each other? Why? // Shouldn't the deleted object point? It turns out that, after the structure analysis, the 42 pointed to by the original p is cleared # include "head. H "class u_ptr {friend class hasptr; int * IP; size_t use; u_ptr (int * P): IP (P), use (1 ){}~ U_ptr () {STD: cout <"end: destructor" <STD: Endl; Delete IP; // The problem is still on the delete pointer .}}; Class hasptr {public: // copy of the values we're givenhasptr (int * P, int I): PTR (New u_ptr (p), Val (I) {STD:: cout <"test: * PTR-> ip is" <* PTR-> ip <STD: Endl;} // constructorhasptr (const hasptr & orig ): PTR (orig. PTR), Val (orig. val) {++ PTR-> use;} // copy members and increment the use count ~ Hasptr () {// Why Program End, destructor, the first one has no hello, and the second one has Hello, because the if statement causes only the last time (as a result, if statement errors that cause program errors are found) if (-- PTR-> Use = 0) {STD: cout <"hello" <STD: Endl; // The problem is finally found, and the test statement is randomly added, cause if judgment to be invalid, solve with curly brackets, delete PTR; // cause of error, judgment to be invalid, resulting in two u_ptr deletions. In fact, only one u_ptr, segmental fault} private is generated: u_ptr * PTR; int Val ;}; int main () {int * P = new int (42); // The problem is not here {hasptr ptr1 (p, 20 ); STD: cout <STD: Endl; hasptr ptr2 (ptr1); hasptr ptr3 (ptr2); hasptr ptr4 (p, 77); // another u_ptr is required, so DELE Te PTR twice, two objects are deleted once, but there is no conflict when pointing to the same one? How many times do I want to delete a file? STD: cout <"---------------" <STD: Endl; int * P2 = P; int * P3 = P; // Delete P2; // both are pointers, delete P2. Delete objects outside the class won't work. You can delete PTR objects several times in the class. Why is this special ~?????!!!} // The error of Delete P is that hasptr is parsed only when it exceeded scope (main), So delete p first; this causes the pointer to overhead in the class, so the method to avoid using a function block STD:: cout <p <":" <* P <STD: Endl; // Delete P ;}

Finally, it is a value type class,

Class hasptr {public: hasptr (int * P, int I): PTR (New int (* p), Val (I) {} hasptr (const hasptr & orig ): PTR (New int (* orig. PTR), Val (orig. val ){}~ Hasptr () {Delete PTR;} hasptr & operator = (const hasptr & RHs) {* PTR = * RHS. PTR; // no new int is required. You only need to assign a value, which is equivalent to the assign operation val = RHS for a normal int object. val; return * This;} // other operation int get_ptr_val () const {return * PTR;} int get_int () const {return val;} void set_ptr (int * P) {PTR = P;} void set_int (int I) {val = I;} // return or change the value pointed to, so OK for const objectsint * get_ptr () const {return PTR;} void set_ptr_val (INT p) const {* PTR = P;} PRIVATE: int * PTR; int val ;};

Q: How do I delete a pointer every time I delete an object? Delete is used to delete objects. Merging destructor is used for automatic revocation of built-in types and class-type destructor calls. But what are built-in type pointers? The book does not explain how to call the destructor of string and other class types if you write this function by yourself.

A: Yes, it is the object to which the Pointer Points. Although each class member is an int * PTR; pointer,Seemingly no integer object is storedIn fact, it is easy to forget the initialization statement,Is a new int object, so that the integer pointer of the Class Object points to the new integer object,The Destructor Delete PTR is also used to delete the int object exclusive to each class object.

Smart pointers are also confused, because different hasptr objects do not necessarily point to the basic integer object through a u_ptr object without the need for replication or other means. Therefore, the delete PTR can be the first u_ptr object or the second u_ptr object.

Because there is no mechanism to check whether the * P used by the newly created hasptr object is repeated, you can only check the count in u_ptr when copying and assigning values.

The initialization list is a pointer directly copied like u_ptr (int * P): IP (P), use (1, in principle, the same basic int object will be pointed to by different u_ptr IP addresses, that is, it can be repeated ~ U_ptr () {Delete IP;}. How can this problem be solved?

Later than 13--28, We need to disable the related replication control functions of Binary Trees. We need to do this around the functions supported by binary trees.

Finally, I doubt that the end address of the hasptr object initialized every time the P is run and later is 008,018,028,038.

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.