Merge pull request #3617 from madebyollin/master

[C++/en] Minor fixes to Smart Pointer section
This commit is contained in:
Divay Prakash 2019-09-01 13:22:37 +05:30 committed by GitHub
commit f766c0aed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -818,51 +818,51 @@ void doSomethingWithAFile(const std::string& filename)
// Smart Pointer // Smart Pointer
///////////////////// /////////////////////
// Generally a smart pointer is a class, which wraps a "raw pointer" (usage of "new" // Generally a smart pointer is a class which wraps a "raw pointer" (usage of "new"
// respectively malloc/calloc in C). The goal is to be able to // respectively malloc/calloc in C). The goal is to be able to
// manage the lifetime of the object being point to without explicitly deleting // manage the lifetime of the object being pointed to without ever needing to explicitly delete
// the object. The term itself simply describes a set of pointers with the // the object. The term itself simply describes a set of pointers with the
// mentioned abstraction. // mentioned abstraction.
// Basically smart pointers should preferred over raw pointers, to prevent // Smart pointers should preferred over raw pointers, to prevent
// risky memory leaks, which happens if you forget to delete the object. // risky memory leaks, which happen if you forget to delete an object.
// Usage of a raw pointer: // Usage of a raw pointer:
Dog* ptr = new Dog(); Dog* ptr = new Dog();
ptr->bark(); ptr->bark();
delete ptr; delete ptr;
// With the usage of smart pointers you dont have to worry about the deletion // By using a smart pointer, you don't have to worry about the deletion
// of a object anymore. // of the object anymore.
// A smart pointer describes a policy, to count the references on the // A smart pointer describes a policy, to count the references to the
// pointer. As matter of fact the objects gets destroyed when the last // pointer. The object gets destroyed when the last
// reference on the object gets destroyed. // reference to the object gets destroyed.
// Usage of "std::shared_ptr": // Usage of "std::shared_ptr":
void foo() void foo()
{ {
// Its not longer necessary to delete the Dog. // It's no longer necessary to delete the Dog.
std::shared_ptr<Dog> doggo(new Dog()); std::shared_ptr<Dog> doggo(new Dog());
doggo->bark(); doggo->bark();
} }
// Beware of possible circular references!!! // Beware of possible circular references!!!
// There will be always a reference, so it will be never destroyed! // There will be always a reference, so it will be never destroyed!
std::shared_ptr<Dog> doggo_one (new Dog()); std::shared_ptr<Dog> doggo_one(new Dog());
std::shared_ptr<Dog> doggo_two (new Dog()); std::shared_ptr<Dog> doggo_two(new Dog());
doggo_one = doggo_two; // p1 references p2 doggo_one = doggo_two; // p1 references p2
doggo_two = doggo_one; // p2 references p1 doggo_two = doggo_one; // p2 references p1
// As mentioned before there is a set of smart pointers. The way you have to // There are several kinds of smart pointers.
// use it, is always the same. // The way you have to use them is always the same.
// This leads us to question, when to use which one? // This leads us to the question: when should we use each kind of smart pointer?
// std::unique_ptr - use it when you just want to hold one reference on // std::unique_ptr - use it when you just want to hold one reference to
// the same object. // the object.
// std::shared_ptr - use it when you want to hold multiple references on the // std::shared_ptr - use it when you want to hold multiple references to the
// same object and want to make sure that it´s de-allocated // same object and want to make sure that it's deallocated
// when all refences are gone. // when all references are gone.
// std::weak_ptr - use it when you want to hold multiple references from // std::weak_ptr - use it when you want to access
// different places for references for which it´s no problem // the underlying object of a std::shared_ptr without causing that object to stay allocated.
// tp de-allocate. // Weak pointers are used to prevent circular referencing.
///////////////////// /////////////////////