From 43fc3289dcf623779a30f1002a58748fe3998812 Mon Sep 17 00:00:00 2001 From: Ollin Boer Bohan Date: Sat, 31 Aug 2019 08:11:27 -0700 Subject: Minor fixes to C++ Smart Pointer section * Fix minor spelling / grammar problems ("tp de-allocate", "refences", "dont"...) * Remove filler words ("Basically", "As a matter of fact"...) * Remove spaces before parens in smart pointer initialization code sample (consistent with the other sections, e.g. the `Tuples` section) * Clarify `std::weak_ptr` use case --- c++.html.markdown | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 176ea1a8..f3dc8e20 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -818,51 +818,51 @@ void doSomethingWithAFile(const std::string& filename) // 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 -// 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 // mentioned abstraction. -// Basically smart pointers should preferred over raw pointers, to prevent -// risky memory leaks, which happens if you forget to delete the object. +// Smart pointers should preferred over raw pointers, to prevent +// risky memory leaks, which happen if you forget to delete an 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. +// By using a smart pointer, you don't have to worry about the deletion +// of the object anymore. +// A smart pointer describes a policy, to count the references to the +// pointer. The object gets destroyed when the last +// reference to the object gets destroyed. // Usage of "std::shared_ptr": void foo() { -// Its not longer necessary to delete the Dog. +// It's no longer necessary to delete the Dog. std::shared_ptr 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 doggo_one (new Dog()); -std::shared_ptr doggo_two (new Dog()); +std::shared_ptr doggo_one(new Dog()); +std::shared_ptr 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. +// There are several kinds of smart pointers. +// The way you have to use them is always the same. +// 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 to +// the object. +// std::shared_ptr - use it when you want to hold multiple references to the +// same object and want to make sure that it's deallocated +// when all references are gone. +// std::weak_ptr - use it when you want to access +// the underlying object of a std::shared_ptr without causing that object to stay allocated. +// Weak pointers are used to prevent circular referencing. ///////////////////// -- cgit v1.2.3 From 5f6375db90cf0dfdbe457fce4239023ed057579e Mon Sep 17 00:00:00 2001 From: Ryan Huang Date: Sat, 23 May 2020 06:42:00 +0000 Subject: Remove extra space --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index f3dc8e20..59aad210 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -202,7 +202,7 @@ int main() cout << "Your favorite number is " << myInt << "\n"; // prints "Your favorite number is " - cerr << "Used for error messages"; + cerr << "Used for error messages"; } ////////// -- cgit v1.2.3 From 79ebb067b36aa96ae6d6daee3f070bf0c895ea73 Mon Sep 17 00:00:00 2001 From: Spencer Burris Date: Sat, 12 Sep 2020 21:06:26 -0700 Subject: Replace newline string with newline character --- c++.html.markdown | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 59aad210..626da194 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -199,7 +199,7 @@ int main() cin >> myInt; // cout can also be formatted - cout << "Your favorite number is " << myInt << "\n"; + cout << "Your favorite number is " << myInt << '\n'; // prints "Your favorite number is " cerr << "Used for error messages"; @@ -461,7 +461,7 @@ void Dog::print() const Dog::~Dog() { - std::cout << "Goodbye " << name << "\n"; + std::cout << "Goodbye " << name << '\n'; } int main() { @@ -504,7 +504,7 @@ void OwnedDog::setOwner(const std::string& dogsOwner) void OwnedDog::print() const { Dog::print(); // Call the print function in the base Dog class - std::cout << "Dog is owned by " << owner << "\n"; + std::cout << "Dog is owned by " << owner << '\n'; // Prints "Dog is and weights " // "Dog is owned by " } @@ -946,7 +946,7 @@ mymap.insert(pair('Z',26)); // To iterate map::iterator it; for (it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << "->" << it->second << '\n'; + std::cout << it->first << "->" << it->second << std::cout; // Output: // A->1 // Z->26 @@ -1117,33 +1117,33 @@ const int maxL = 15; auto second = make_tuple(maxN, maxL); // Printing elements of 'first' tuple -cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A +cout << get<0>(first) << " " << get<1>(first) << '\n'; //prints : 10 A // Printing elements of 'second' tuple -cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 +cout << get<0>(second) << " " << get<1>(second) << '\n'; // prints: 1000000000 15 // Unpacking tuple into variables int first_int; char first_char; tie(first_int, first_char) = first; -cout << first_int << " " << first_char << "\n"; // prints : 10 A +cout << first_int << " " << first_char << '\n'; // prints : 10 A // We can also create tuple like this. tuple third(11, 'A', 3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) -cout << tuple_size::value << "\n"; // prints: 3 +cout << tuple_size::value << '\n'; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. auto concatenated_tuple = tuple_cat(first, second, third); // concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A', 3.14141) -cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 -cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' +cout << get<0>(concatenated_tuple) << '\n'; // prints: 10 +cout << get<3>(concatenated_tuple) << '\n'; // prints: 15 +cout << get<5>(concatenated_tuple) << '\n'; // prints: 'A' /////////////////////////////////// -- cgit v1.2.3 From e07193e6b463a70a70d58bcaf2936362d9d96e75 Mon Sep 17 00:00:00 2001 From: Priti Xavier Date: Tue, 29 Sep 2020 20:50:47 +0800 Subject: Changed operator < to != (#4004) --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 626da194..948b52ec 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -915,7 +915,7 @@ ST.erase(20); // Will erase element with value 20 // Set ST: 10 30 // To iterate through Set we use iterators set::iterator it; -for(it=ST.begin();it