小 议 Auto_PTR
I have always thought that auto_ptr is a easy-to-use thing. Although I have never used it. Until yesterday, I finally used the VC version of Auto_PTR. There are many behaviors that make it a lot of behavior. I have re-put the STL reference book. Subsequently, I turned into a VC version of the auto_ptr source code. It turned out to be .....
Have what, summarized some of the following stuff, looking for beginners to help. At the same time, if there is something wrong, you will see more pragmatism. Detailed explanation, the code annotation!
//AUTO_PTR.H
#ifndef auto_ptr__h __ # define auto_ptr__h__
// Declaration: // 1. The following source code is for learning only. The consequences caused during any use are not related to the author. If you are used, you accept the declaration
// Author's Ank Bin Liang // Time 2003.06.21 // Email: kinglinux@163.com
// vc auto_ptr: A auto_ptrnamespace vc {template
PUBLIC: T & Operator * () const {return (* _ptr);
T * Operator -> () const {return (_ptr);
PUBLIC: T * Release () const {((Auto_Ptr
Private: bool _own; // Record whether there is owned T *_ptr; // Reference pointer};}; // namespace VC
// stl auto_ptr: modeled auto_ptr behavior STL version written a auto_ptrnamespace stl {template
PUBLIC: T & Operator * () const {return (* _ptr);
T * Operator -> () const {return (_ptr);
Private: t * _ptr; // Point points to the pointer with ownership objects};}; // namespace STL
// The following version is referring to the << C Standard Library >> (The C Standard Library) // Nicolai M.JOSUTTISTIS Hou Jiemeng Hongyan Translation of the September // Book P222 6.8 Proposed the viewpoint of Reference's Auto_PTR One version // ref auto_ptr: namespace ref {template
T & Operator * () const {return (* _ptr);
T * Operator -> () const {return (_ptr);
Unsigned long * addref (void) // increase the reference count { (* _ count); return (_count);}
Private: void release (void) {if (* (* _ count) == 0) // Reduce the reference count and test the reference count is 0 {delete _ptr; delete _count;} _count = null; // Release reference counter _PTR = NULL; // Release ownership} private: t * _ptr; // Reference Object Pointer Unsigned long * _count; // Reference counter pointer};}; // Namespace Ref
// The above three versions are simply implemented by the generally accepted AUTO_PTR behavior now. The application is better to use the AUTO_PTR with reference to the reference count.
// There are still many ways to implement Auto_Ptr, especially in different applications // to change Auto_PTR behavior based on different applications, allowing it to make it more suitable for our app. You can use the base class to do a reference count to achieve secure // Auto_Ptr // of the reference count, and can be used as a reference count to be loaded with a reference count to implement //. AUTO_PTR
// the end.
#ENDIF // Auto_PTR__H__
/ / The following is the test program under VC6
//dog.h
#ifndef dog__h __ # define Dog__h__
#include
// Test class class dog {public: Dog (std :: string name = "noname"): _ name (name) {std :: cout << "dog :: DOG ()" << std :: end1
~ DOG () {std :: cout << "Dog :: ~ DOG () << std :: endl;}
Public: void bark (void) {std :: cout << _ name << "Always Bark At Rose." << std :: endl;}
Public: void name (const st :: string & name) {_name = name;
Std: string name (void) {return_name;}
PRIVATE: std :: string _name;
#ENDIF / / DOG__H__
//main.cpp
#include
#include "auto_ptr.h" #include "dog.h"
/// Test vc auto_ptrvoid test_vc_fun (void) {std :: cout << "=== Test vc auto_ptr ===" << std :: end1
Vc :: auto_ptr
{Vc :: auto_ptr
/// Test STL AUTO_PTRVOID TEST_STL_FUN (VOID) {std :: cout << "=== Test STL Auto_PTR ===" << std :: endl; stl :: auto_ptr
{Stl :: auto_ptr
/// Test ref auto_ptrvoid test_ref_fun (void) {std :: cout << "=== Test ref auto_ptr ===" << std :: end1
Ref :: auto_ptr
{Ref :: auto_ptr
PTOM-> bark (); // successful}
Void main (void) {std :: cout << "Hello, The World!" << std :: end1; test_vc_fun (); test_stl_fun (); test_ref_fun ();
// the end