Previous: http://www.bkjia.com/kf/201112/115672.html
17.1.9 auto_ptr class
The auto_ptr class is defined in the header file memory.
Auto_ptr can only be used to manage one object returned from new. It cannot manage dynamically allocated arrays.
As we can see, when auto_ptr is copied or assigned a value, there is an unusual behavior, so auto_ptr cannot be stored in the standard library container type.
Each auto_ptr object is bound to an object or points to an object. When the auto_ptr object points to an object, it can be said that it "owns" the object. When the auto_ptr object exceeds the scope or is revoked separately, the dynamic memory object pointed to by auto_ptr is automatically reclaimed.
1. Use auto_ptr for abnormal and secure memory allocation
Auto_ptr <int> I (new int (42); // auto_ptr manage int * I...
Throw exception ();
Auto_ptr <int> I (new int (42); // auto_ptr manage int * I...
Throw exception (); 2. auto_ptr is a template that can save any type of pointer.
The auto_ptr class is a template that accepts a single type of parameter. This type specifies the type of the object that auto_ptr can bind. Therefore, you can create auto_ptr of any type.
3. Bind auto_ptr to pointer
The constructor that accepts pointers is an explicit constructor. Therefore, you must create an auto_ptr object in the form of initialization.
4. Use auto_ptr object
Auto_ptr <string> I (new string ("42"); // auto_ptr manage string * I...
// Throw exception ();
Cout <* I <endl;
Cout <I-> length () <endl;
Auto_ptr <string> I (new string ("42"); // auto_ptr manage string * I...
// Throw exception ();
Cout <* I <endl;
The main purpose of cout <I-> length () <endl; auto_ptr is to ensure that the objects referenced by the auto_ptr object are automatically deleted, while common pointer actions are supported. As we can see, the fact that the object is automatically deleted leads to a significant difference between auto_ptr and common pointers in how to copy and access their address values.
5. Copying and assigning auto_ptr objects are destructive operations.
Auto_ptr and built-in pointers are very important differences between copying and assigning values. When the auto_ptr object is copied or its value is assigned to another auto_ptr object, the ownership of the basic object is transferred from the original auto_ptr object to the copy, and the original auto_ptr object is reset to the unbound state.
Unlike other copy or assign values, the copy and assign values of auto_ptr change the right operand. Therefore, the left and right operands of the assign values must be modifiable left values.
6. assign a value to delete the object pointed to by the left operand
In addition to transferring ownership from the right operand to the left operand, the assignment also deletes the object originally pointed to by the left operand-if two objects are different. Generally, the assignment is ineffective.
Auto_ptr objects cannot be stored in standard containers because of destructive operations during copying and assigning values. The container class of the standard library requires that the two objects after copying or assigning values are equal. auto_ptr does not meet this requirement.
7. default constructor of auto_ptr
If the initial formula is not given, the auto_ptr object is not bound and does not point to the object.
By default, the internal pointer value of auto_ptr is set to 0. Unreferencing unbound auto_ptr objects has the same effect as unbinding pointer-program error and no definition of what will happen.
8. Test the auto_ptr object
To check whether the pointer is not bound, you can directly test the pointer in the condition to determine whether the pointer is 0. On the contrary, you cannot directly test the auto_ptr object.
The auto_ptr type does not define the type conversion that can be used as a condition. On the contrary, to test the auto_ptr object, you must use its get Member, which contains the basic pointer in the auto_ptr object.
Auto_ptr <string> I (new string ("42"); // auto_ptr manage string * I...
// Throw exception ();
If (I. get ()){
Cout <* I <endl;
Cout <I-> length () <endl;
}
Auto_ptr <string> I (new string ("42"); // auto_ptr manage string * I...
// Throw exception ();
If (I. get ()){
Cout <* I <endl;
Cout <I-> length () <endl;
} To determine whether auto_ptr points to an object, you can compare the get return value with 0.
You should only use get to query the auto_ptr object or use the returned pointer value. You cannot use get as the real parameter for creating other auto_ptr objects.
Using get members to initialize other auto_ptr objects violates the auto_ptr class design principle: Only one auto_ptr object saves the given pointer at any time. If two auto_ptr objects Save the same pointer, the pointer is deleted twice.
9. reset operation
Another difference between the auto_ptr object and the built-in pointer is that you cannot directly assign an address (or other pointers) to the auto_ptr object. Instead, you must call the reset function to change the pointer.
When the reset function of the auto_ptr function object is called, the object pointed to by the auto_ptr object will be deleted (if any) before the auto_ptr object is bound to other objects ). However, just as the value assignment is ineffective, if the reset function of the same pointer already saved in the auto_ptr object is called, the object will not be deleted.
Auto_ptr <string> I (new string ("42"); // auto_ptr manage string * I...
// Throw exception ();
If (I. get ()){
I. reset (new string ("Anders "));
Cout <* I <endl; // Anders
Cout <I-> length () <endl; // 6
}
From xufei96's column