Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ 11 smart pointers     - How to contribute code to the GNU Project (Linux)

- pureftpd basis: Install, configure, implement, anonymous logon (Linux)

- RHEL 7.1 compile and install Ganglia 3.7.1 (Server)

- SSH Filesystem use a secure connection for network file system (Linux)

- Linux partition command (Linux)

- Which file system is most suitable for your Linux system (Linux)

- How to Set Free SSH password on CentOS / RHEL (Linux)

- Elasticsearch 2.20 Highlight (Server)

- Briefly explain the invasion of the four-level denial of service attack DoS Linux server (Linux)

- Chromium Install Flash Official Guide (Linux)

- PF_RING 6.0.2 compiled and installed on Redhat 6.3 x86_64 (Linux)

- How to use the Linux terminal Git commands (Linux)

- Executable file format Explanation under Linux (Linux)

- Linux-- sub-volume compression and decompression (Linux)

- MySQL 5.5 on master-slave copy filter (Database)

- To install Ganglia configuration of experience under CentOS 5.5 (Linux)

- Linux operating system boot process analysis (Linux)

- MariaDB database storage path modify configuration issues (Database)

- Help you make Git Bisect (Linux)

- Use smem visual display Linux memory usage (Linux)

  C ++ 11 smart pointers
  Add Date : 2018-11-21      
  In C ++ 11, the introduction of smart pointers. There are: unique_ptr, shared_ptr, weak_ptr.
These three components is to use a pointer in the smart pointer boost scheme. Many have used the boost smart pointers friends, you can easily find off between them:

std boost Function
unique_ptr scoped_ptr exclusive object pointer, and the pointer to ensure that the life cycle of an object consistent with
shared_ptr shared_ptr can be shared pointer object can be assigned to shared_ptr or weak_ptr.
The object pointer at the end of the life cycle of all shared_ptr associated with the end of a strong reference.
weak_ptr weak_ptr it can not determine the referent of the life cycle, when referring to the referent, we need lock () to use a shared_ptr.
C ++ 11 will boost included in this set of standards.

Sample code is as follows:

// File test-1.cpp
#include < memory>
#include < iostream>

using namespace std;

int main ()
    unique_ptr up1 (new int (11));
    unique_ptr up2 = up1;! // compile time error [1]

    cout << * up1 << endl;
    unique_ptr up3 = move (up1); // [2]!
    cout << * up3 << endl;
    if (up1)
        cout << * up1 << endl;

    up3.reset (); // [3]!
    up1.reset ();

    shared_ptr sp1 (make_shared ( "Hello"));
    shared_ptr sp2 = sp1;
    cout << "* sp1:" << * sp1 << endl;
    cout << "* sp2:" << * sp2 << endl;
    sp1.reset ();
    cout << "* sp2:" << * sp2 << endl;

    weak_ptr wp = sp2; // [4]!
    cout << "* wp.lock ():" << * wp.lock () << endl;
    sp2.reset ();
    cout << "* wp.lock ():" << * wp.lock () << endl; // will run-time error!
    return 0;
// Compile command: g ++ -std = c ++ 11 test-1.cpp
[1]: unique_ptr copy assignment is prohibited, and always maintain a unique_ptr manage an object.
[2]: unique_ptr although can not be assigned, but you can () function through the transfer of ownership of the object move. Once the move (), the original up1 is no longer valid.
[3]: reset () allows unique_ptr early release pointer.
[4]: a weak_ptr by the shared_ptr constructor.

shared_ptr and weak_ptr

As in the following example:

shared_ptr < string> s1 (new string);
shared_ptr < string> s2 = s1;
weak_ptr < string> w1 = s2;

s1, s2, w1 ptr_manage a point object.
There shared_ref_count and weak_ref_count two domains are recorded with reference to the number of its shared_ptr weak_ptr in this object. This is very easy to do, just copy constructor and assignment of function rather quoted value plus 1, minus 1 to destructor. ptr_manage the ptr field contains the real object pointer address.

When shared_ref_cnt was reduced to 0, the object is automatically released ptr pointer points. When shared_ref_cnt with weak_ref_cnt have become 0 before the release ptr_manage object.
So since, as long as shared_ptr associated with the presence of the object exists. weak_ptr does not affect the lifetime of the object. When used weak_ptr to access the object, the object may have already been released, the first lock ().

When performing:

s1.reset ()
at this time:

shared_ref_cnt by two became minus 1.

Then execute:

s2.reset ()
at this time:

shared_ref_cnt has been reduced to a 0, ptr corresponding object has been released, ptr is cleared. At this time, ptr_manage still retained. Because w1 also need to refer to it.

In the final, w1 also destructed when:

ptr_manage in weak_ref_cnt is reduced to zero, and finally even ptr_manage are released.
- File encryption and decryption of Linux security mechanisms (Linux)
- Linux environment to build next Cocos2dx-3.3.1 (Linux)
- Linux, Oracle listen address modification (Database)
- Construction LVM-based raw device Oracle10g Database on Oracle Linux 5.11 (Database)
- To install and configure the Jetty server and JDK under Ubuntu 14.04.2 (Server)
- Linux System Getting Started Learning: After starting in Ubuntu or Debian, enter the command line (Linux)
- Character Encoding notes: ASCII, Unicode and UTF-8 (Linux)
- How to install MySQL on Linux Dock (Database)
- Linux Network Security Tips Share (Linux)
- Linux server remote user to obtain root privileges prevention (Linux)
- Binary Tree Traversal (Linux)
- After SSH change the default port, how to use Git (Linux)
- Vi editor Introduction (Linux)
- Linux System Getting Started Learning: Disable HTTP forwarding wget in (Linux)
- TL-WR703N to install OpenWrt process notes (Linux)
- Thrift 0.9.3 compiler installation under Ubuntu (Linux)
- How to update the ISPConfig 3 SSL Certificates (Server)
- Python pickle module for simple use notes (Programming)
- KVM virtualization nested configuration (Server)
- CentOS source installation GitLab Chinese Version (Server)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.