summaryrefslogtreecommitdiffhomepage
path: root/c++.html.markdown
diff options
context:
space:
mode:
authorDimitri Kokkonis <kokkonisd@gmail.com>2019-08-06 12:40:31 +0200
committerGitHub <noreply@github.com>2019-08-06 12:40:31 +0200
commit922fc494bcce6cb53d80a5c2c9c039a480c82c1f (patch)
treea62b6e194db73630bfed43301b1c130d52f9dbba /c++.html.markdown
parent5710394756a426255d2dc81d2d342e9786ac2c1b (diff)
parent2f0b904f6ffe68d15fedf7e50a3a64e1a47a9145 (diff)
Merge pull request #1 from adambard/master
Update fork
Diffstat (limited to 'c++.html.markdown')
-rw-r--r--c++.html.markdown63
1 files changed, 59 insertions, 4 deletions
diff --git a/c++.html.markdown b/c++.html.markdown
index 80ad3a6c..176ea1a8 100644
--- a/c++.html.markdown
+++ b/c++.html.markdown
@@ -72,7 +72,7 @@ void func(); // function which may accept any number of arguments
int* ip = nullptr;
// C standard headers are available in C++.
-// C headers end in .h, while
+// C headers end in .h, while
// C++ headers are prefixed with "c" and have no ".h" suffix.
// The C++ standard version:
@@ -553,10 +553,14 @@ Point Point::operator+(const Point& rhs) const
return Point(x + rhs.x, y + rhs.y);
}
+// It's good practice to return a reference to the leftmost variable of
+// an assignment. `(a += b) == c` will work this way.
Point& Point::operator+=(const Point& rhs)
{
x += rhs.x;
y += rhs.y;
+
+ // `this` is a pointer to the object, on which a method is called.
return *this;
}
@@ -757,7 +761,7 @@ failure:
// things are a little cleaner, but still sub-optimal.
void doSomethingWithAFile(const char* filename)
{
- FILE* fh = fopen(filename, "r"); // Open the file in read mode
+ FILE* fh = fopen(filename, "r"); // Open the file in shared_ptrread mode
if (fh == nullptr)
throw std::runtime_error("Could not open the file.");
@@ -811,6 +815,57 @@ void doSomethingWithAFile(const std::string& filename)
/////////////////////
+// Smart Pointer
+/////////////////////
+
+// 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
+// manage the lifetime of the object being point to without explicitly deleting
+// the object. The term itself simply describes a set of pointers with the
+// mentioned abstraction.
+// Basically smart pointers should preferred over raw pointers, to prevent
+// risky memory leaks, which happens if you forget to delete the object.
+
+// Usage of a raw pointer:
+Dog* ptr = new Dog();
+ptr->bark();
+delete ptr;
+
+// With the usage of smart pointers you dont have to worry about the deletion
+// of a object anymore.
+// A smart pointer describes a policy, to count the references on the
+// pointer. As matter of fact the objects gets destroyed when the last
+// reference on the object gets destroyed.
+
+// Usage of "std::shared_ptr":
+void foo()
+{
+// Its not longer necessary to delete the Dog.
+std::shared_ptr<Dog> doggo(new Dog());
+doggo->bark();
+}
+
+// Beware of possible circular references!!!
+// 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_two (new Dog());
+doggo_one = doggo_two; // p1 references p2
+doggo_two = doggo_one; // p2 references p1
+
+// As mentioned before there is a set of smart pointers. The way you have to
+// use it, is always the same.
+// This leads us to question, when to use which one?
+// std::unique_ptr - use it when you just want to hold one reference on
+// the same object.
+// std::shared_ptr - use it when you want to hold multiple references on the
+// same object and want to make sure that it´s de-allocated
+// when all refences are gone.
+// std::weak_ptr - use it when you want to hold multiple references from
+// different places for references for which it´s no problem
+// tp de-allocate.
+
+
+/////////////////////
// Containers
/////////////////////
@@ -873,7 +928,7 @@ cout << ST.size(); // will print the size of set ST
// Output: 0
// NOTE: for duplicate elements we can use multiset
-// NOTE: For hash sets, use unordered_set. They are more efficient but
+// NOTE: For hash sets, use unordered_set. They are more efficient but
// do not preserve order. unordered_set is available since C++11
// Map
@@ -902,7 +957,7 @@ cout << it->second;
// Output: 26
-// NOTE: For hash maps, use unordered_map. They are more efficient but do
+// NOTE: For hash maps, use unordered_map. They are more efficient but do
// not preserve order. unordered_map is available since C++11.
// Containers with object keys of non-primitive values (custom classes) require