What is the smart pointer of c/c + + standard library

What is the smart pointer of standard library

1, Why is there a smart pointer???

c + + programmers need to deal with their own dynamic development of memory, once they forget to release, memory will leak.

Smart pointers can help programmers "free up" their own memory.

2, Where do you see intelligence???

int *p = new int(11);
auto_ptr<int> pa(p);//Auto PTR is not recommended
//delete p;

After the above code gives p to the intelligent pointer auto PTR for management, you don't need to delete p yourself. Auto PTR will release P, so it reflects "intelligence".

3, Where does it look like a pointer???

int *p = new int(11);
my_auto_ptr<int> pa(p);                                                                
*pa = 111;
cout << *pa << endl;
cout << *p << endl;

The above code uses the * operator for the smart pointer pa, and changes the value of p through pa, so it looks like a pointer.

class Test{
public:
  void fun(){
    cout << "func()" << endl;
  }
};

Test* pt = new Test;
auto_ptr<Test> pa1(pt);
pa1->fun();

The above code uses the, - > operator for the smart pointer pa1, and calls the fun() member method of the object pt through pa1, so it looks like a pointer.

4, What is a smart pointer???

Is a template class.

5, How does intelligent pointer realize intelligence and pointer?

  • Override operator * operator in template class
  • Override operator - > operator in template class
  • Manage ownership and transfer of pointers (not implemented in the following example)
#include <iostream>
#include <memory>

using namespace std;

template<typename T>
class my_auto_ptr{
public:
  my_auto_ptr(T* p = nullptr):own(p!=nullptr),ptr(p){}
  ~my_auto_ptr(){
    if(own){
      delete ptr;
    }
  }
  T& operator*()const{
    return *ptr;
  }
  T* operator->()const{
    return ptr;
  }
private:
  bool own;
  T* ptr;
};
class Test{
public:
  void fun(){
    cout << "func()" << endl;
  }
};
int main(){
  //It is not recommended to use the old version of auto ﹣ PTR of test1                                
  /*                                                                            
  int *p = new int(10);                                                         
  auto_ptr<int> pa(p);                                                          
  cout << *pa << endl;                                                          
                                                                                
  string* ps = new string("aaa");                                               
  auto_ptr<string> pas(ps);                                                     
  cout << pas->size() << endl;                                                  
  */

  //test2 implements auto PTR by itself                                                      
  int *p = new int(11);
  my_auto_ptr<int> pa(p);
  //delete p;                                                                   
  *pa = 111;
  cout << *pa << endl;
  cout << *p << endl;

  Test* pt = new Test;
  my_auto_ptr<Test> pa1(pt);
  pa1->fun();

  return 0;
}

github full code

QQ group of mutual learning in c/c + +: 877684253

My wechat: xiaoshitou5854

Keywords: C++ github

Added by akillez on Thu, 26 Dec 2019 00:11:02 +0200