Shared Pointer   A  shared  pointer is a smart pointer that allows for distributed ownership of data. Each time it is assigned, a  reference count  is incremented, indicating one more owner of the data. When the pointer goes out of scope or an owner calls  reset , the reference count is decremented. When the reference count goes to 0, the data pointed to is deallocated. Use  make_shared :  auto  person =  std :: make_shared < Person > ( ) ;  if ( person )  {      cout <<  "Person's address is "  <<  person -> address ;  }    C++17 allows use of shared pointers to point to dynamically allocated arrays. However,  make_shared  cannot be used. Below is an example:  shared_ptr < Person [ ] >  persons ( new  Person [ 10 ] ) ;  persons [ 0 ] . _name =  "Jack Sparrow" ;  persons [ 0 ] . _address =  "Caribbean" ;  cout <<  "Address of first person " << persons [ 0 ] . address <<  endl ;    A better solutio...
 
Comments
Post a Comment