From bf493c07ed519cc3d41198a0720a5919f67f3ff0 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 1 Dec 2014 21:39:49 -0700 Subject: Reassignment to reference doesn't cause error --- c++.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 9f357b08..5f80f26f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -238,7 +238,10 @@ string& fooRef = foo; // This creates a reference to foo. fooRef += ". Hi!"; // Modifies foo through the reference cout << fooRef; // Prints "I am foo. Hi!" -fooRef = bar; // Error: references cannot be reassigned. +// Doesn't reassign "fooRef". This is the same as "foo = bar", and +// foo == "I am bar" +// after this line. +fooRef = bar; const string& barRef = bar; // Create a const reference to bar. // Like C, const values (and pointers and references) cannot be modified. -- cgit v1.2.3 From ebdde372443c0fe51b2513c9bfcfad8378ae52cd Mon Sep 17 00:00:00 2001 From: Riku-Pekka Silvola Date: Wed, 25 Feb 2015 18:10:57 +0100 Subject: fixed some typos --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 5f80f26f..67fa054c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -492,7 +492,7 @@ bool doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); // Open the file in read mode if (fh == nullptr) // The returned pointer is null on failure. - reuturn false; // Report that failure to the caller. + return false; // Report that failure to the caller. // Assume each function returns false if it failed if (!doSomethingWithTheFile(fh)) { @@ -513,7 +513,7 @@ bool doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); if (fh == nullptr) - reuturn false; + return false; if (!doSomethingWithTheFile(fh)) goto failure; -- cgit v1.2.3 From c88c28058b34c84de853694eb71d402fee937168 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Fri, 13 Mar 2015 23:40:45 +0800 Subject: Closes #1000 --- c++.html.markdown | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 67fa054c..1978d183 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -30,10 +30,10 @@ one of the most widely-used programming languages. // C++ is _almost_ a superset of C and shares its basic syntax for // variable declarations, primitive types, and functions. -// However, C++ varies in some of the following ways: -// A main() function in C++ should return an int, -// though void main() is accepted by most compilers (gcc, clang, etc.) +// Just like in C, your program's entry point is a function called +// main with an integer return type, +// though void main() is also accepted by most compilers (gcc, clang, etc.) // This value serves as the program's exit status. // See http://en.wikipedia.org/wiki/Exit_status for more information. int main(int argc, char** argv) @@ -51,6 +51,8 @@ int main(int argc, char** argv) return 0; } +// However, C++ varies in some of the following ways: + // In C++, character literals are one byte. sizeof('c') == 1 -- cgit v1.2.3 From 299f3de0eacfe4e3d1303135716ad14a09bb0118 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Sat, 11 Apr 2015 12:52:23 +0800 Subject: [c++/en] Fix spelling --- 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 1978d183..1a84efa4 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -555,7 +555,7 @@ void doSomethingWithAFile(const char* filename) // Compare this to the use of C++'s file stream class (fstream) // fstream uses its destructor to close the file. // Recall from above that destructors are automatically called -// whenver an object falls out of scope. +// whenever an object falls out of scope. void doSomethingWithAFile(const std::string& filename) { // ifstream is short for input file stream -- cgit v1.2.3 From 7d5368eda1e2402b6b2bed85586fde0c6af87816 Mon Sep 17 00:00:00 2001 From: MoreMoschops Date: Sun, 26 Apr 2015 15:33:29 +0100 Subject: Neither gcc nor clang accept void main. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Neither gcc nor clang accept void main. Remove this bad information. Tested as follows: $ cat 045.cpp void main() { } $ g++ 045.cpp 045.cpp:1:11: error: ‘::main’ must return ‘int’ void main() ^ $ clang++ 045.cpp 045.cpp:1:1: error: 'main' must return 'int' void main() ^~~~ int 1 error generated. $ g++ --version g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2 $ clang++ --version Ubuntu clang version 3.4.2-3ubuntu2~xedgers (tags/RELEASE_34/dot2-final) (based on LLVM 3.4.2) --- c++.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1a84efa4..ae93ceba 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -32,8 +32,7 @@ one of the most widely-used programming languages. // variable declarations, primitive types, and functions. // Just like in C, your program's entry point is a function called -// main with an integer return type, -// though void main() is also accepted by most compilers (gcc, clang, etc.) +// main with an integer return type. // This value serves as the program's exit status. // See http://en.wikipedia.org/wiki/Exit_status for more information. int main(int argc, char** argv) -- cgit v1.2.3 From c21cf5a1e230dadf81bc4e31e2d2f9133551b365 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 4 May 2015 02:40:04 -0600 Subject: Templates and such --- c++.html.markdown | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ae93ceba..10c39c9c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -432,6 +432,81 @@ int main () { return 0; } +///////////////////// +// Templates +///////////////////// + +// Templates in C++ are mostly used for generic programming, though they are +// much more powerful than generics constructs in other languages. It also +// supports explicit and partial specialization, functional-style type classes, +// and also it's Turing-complete. + +// We start with the kind of generic programming you might be familiar with. To +// define a class or function that takes a type parameter: +template +class Box { + // In this class, T can be used as any other type. + void insert(const T&) { ... } +}; + +// During compilation, the compiler actually generates copies of each template +// with parameters substituted, and so the full definition of the class must be +// present at each invocation. This is why you will see template classes defined +// entirely in header files. + +// To instantiate a template class on the stack: +Box intBox; + +// and you can use it as you would expect: +intBox.insert(123); + +// You can, of course, nest templates: +Box > boxOfBox; +boxOfBox.insert(intBox); + +// Up until C++11, you muse place a space between the two '>'s, otherwise '>>' +// will be parsed as the right shift operator. + +// You will sometimes see +// template +// instead. The 'class' keyword and 'typename' keyword are _mostly_ +// interchangeable in this case. For full explanation, see +// http://en.wikipedia.org/wiki/Typename +// (yes, that keyword has its own Wikipedia page). + +// Similarly, a template function: +template +void barkThreeTimes(const T& input) +{ + input.bark(); + input.bark(); + input.bark(); +} + +// Notice that nothing is specified about the type parameters here. The compiler +// will generate and then type-check every invocation of the template, so the +// above function works with any type 'T' that has a const 'bark' method! + +Dog fluffy; +fluffy.setName("Fluffy") +barkThreeTimes(fluffy); Prints "Fluffy barks" three times. + +// Template parameters don't have to be classes, though this is used very rarely: +template +void printMessage() { + cout << "Learn C++ in " << Y << " minutes!" << endl; +} + +// And you can explicitly specialize templates for more efficient code: +template<> +void printMessage<10>() { + cout << "Learn C++ faster in only 10 minutes!" << endl; +} + +printMessage<20>(); // Prints "Learn C++ in 20 minutes!" +printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" + + ///////////////////// // Exception Handling ///////////////////// @@ -585,6 +660,33 @@ void doSomethingWithAFile(const std::string& filename) // vector (i.e. self-resizing array), hash maps, and so on // all automatically destroy their contents when they fall out of scope. // - Mutexes using lock_guard and unique_lock + + +///////////////////// +// Fun stuff +///////////////////// + +// Aspects of C++ that may be surprising to newcomers (and even some veterans): + +// You can override private methods! +class Foo { + virtual void bar(); +}; +class FooSub : public Foo { + virtual void bar(); // overrides Foo::bar! +}; + +// 0, false, NULL are all the same thing! +bool* pt = new bool; +*pt = 0; // Sets the value points by 'pt' to false. +pt = 0; // Sets 'pt' to the null pointer. Yes both lines compile without warning. + +// '=' != '=' +Foo f1 = f2; // Calls Foo::Foo(const Foo&) or some variant copy constructor. + +Foo f1; +f1 = f2; // Calls Foo::operator=(Foo&) or variant. + ``` Futher Reading: -- cgit v1.2.3 From 05c2617aaf6d040b1e4372d8f83df11288ff2279 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 4 May 2015 02:45:31 -0600 Subject: Minor fix --- c++.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 10c39c9c..e50c33fa 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -489,15 +489,16 @@ void barkThreeTimes(const T& input) Dog fluffy; fluffy.setName("Fluffy") -barkThreeTimes(fluffy); Prints "Fluffy barks" three times. +barkThreeTimes(fluffy); // Prints "Fluffy barks" three times. -// Template parameters don't have to be classes, though this is used very rarely: +// Template parameters don't have to be classes: template void printMessage() { cout << "Learn C++ in " << Y << " minutes!" << endl; } -// And you can explicitly specialize templates for more efficient code: +// And you can explicitly specialize templates for more efficient code (most +// real-world uses of specialization are not as trivial as this): template<> void printMessage<10>() { cout << "Learn C++ faster in only 10 minutes!" << endl; -- cgit v1.2.3 From 6c635d1c3e5bb88940fb0afb01218d2e7d3f11c0 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Tue, 5 May 2015 00:45:14 -0600 Subject: Response to comments --- c++.html.markdown | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index e50c33fa..517ce367 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -497,8 +497,10 @@ void printMessage() { cout << "Learn C++ in " << Y << " minutes!" << endl; } -// And you can explicitly specialize templates for more efficient code (most -// real-world uses of specialization are not as trivial as this): +// And you can explicitly specialize templates for more efficient code. Of +// course, most real-world uses of specialization are not as trivial as this. +// Note that you still need to declare the function (or class) as a template +// even if you explicitly specified all parameters. template<> void printMessage<10>() { cout << "Learn C++ faster in only 10 minutes!" << endl; @@ -667,7 +669,9 @@ void doSomethingWithAFile(const std::string& filename) // Fun stuff ///////////////////// -// Aspects of C++ that may be surprising to newcomers (and even some veterans): +// Aspects of C++ that may be surprising to newcomers (and even some veterans). +// This section is, unfortunately, wildly incomplete; C++ is one of the easiest +// languages with which to shoot yourself in the foot. // You can override private methods! class Foo { @@ -677,16 +681,35 @@ class FooSub : public Foo { virtual void bar(); // overrides Foo::bar! }; -// 0, false, NULL are all the same thing! + +// 0 == false == NULL (most of the time)! bool* pt = new bool; *pt = 0; // Sets the value points by 'pt' to false. -pt = 0; // Sets 'pt' to the null pointer. Yes both lines compile without warning. +pt = 0; // Sets 'pt' to the null pointer. Both lines compile without warnings. + +// nullptr is supposed to fix some of that issue: +int* pt2 = new int; +*pt2 = nullptr; // Doesn't compile +pt2 = nullptr; // Sets pt2 to null. + +// But somehow 'bool' type is an exception. +*pt = nullptr; // This still compiles, even though '*pt' is a bool! + + +// '=' != '=' != '='! +// Calls Foo::Foo(const Foo&) or some variant copy constructor. +Foo f2; +Foo f1 = f2; -// '=' != '=' -Foo f1 = f2; // Calls Foo::Foo(const Foo&) or some variant copy constructor. +// Calls Foo::Foo(const Foo&) or variant, but only copies the 'Foo' part of +// 'fooSub'. Any extra members of 'fooSub' are discarded. This lovely behavior +// is called "object slicing." +FooSub fooSub; +Foo f1 = fooSub; +// Calls Foo::operator=(Foo&) or variant. Foo f1; -f1 = f2; // Calls Foo::operator=(Foo&) or variant. +f1 = f2; ``` Futher Reading: -- cgit v1.2.3 From 25bd06d77a70964acfbdbf8a7c7a50eb312eae9f Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Wed, 6 May 2015 15:25:28 -0600 Subject: comment changes --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 517ce367..66d4aeb1 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -692,7 +692,7 @@ int* pt2 = new int; *pt2 = nullptr; // Doesn't compile pt2 = nullptr; // Sets pt2 to null. -// But somehow 'bool' type is an exception. +// But somehow 'bool' type is an exception (this is to make `if (ptr)` compile). *pt = nullptr; // This still compiles, even though '*pt' is a bool! @@ -702,8 +702,8 @@ Foo f2; Foo f1 = f2; // Calls Foo::Foo(const Foo&) or variant, but only copies the 'Foo' part of -// 'fooSub'. Any extra members of 'fooSub' are discarded. This lovely behavior -// is called "object slicing." +// 'fooSub'. Any extra members of 'fooSub' are discarded. This sometimes +// horrifying behavior is called "object slicing." FooSub fooSub; Foo f1 = fooSub; -- cgit v1.2.3 From bad283ee14dadd02f9b21d5fbcff226bfb5c954f Mon Sep 17 00:00:00 2001 From: Hans Ole Hatzel Date: Wed, 20 May 2015 19:38:11 +0200 Subject: [c++/en] Fixed typos. --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 66d4aeb1..9f8f5f32 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -425,7 +425,7 @@ int main () { Point up (0,1); Point right (1,0); // This calls the Point + operator - // Point up calls the + (function) with right as its paramater + // Point up calls the + (function) with right as its parameter Point result = up + right; // Prints "Result is upright (1,1)" cout << "Result is upright (" << result.x << ',' << result.y << ")\n"; @@ -464,7 +464,7 @@ intBox.insert(123); Box > boxOfBox; boxOfBox.insert(intBox); -// Up until C++11, you muse place a space between the two '>'s, otherwise '>>' +// Up until C++11, you must place a space between the two '>'s, otherwise '>>' // will be parsed as the right shift operator. // You will sometimes see @@ -712,7 +712,7 @@ Foo f1; f1 = f2; ``` -Futher Reading: +Further Reading: An up-to-date language reference can be found at -- cgit v1.2.3 From 462ac892179d64437b1124263402378a6054e50b Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 31 May 2015 21:38:03 -0400 Subject: Remove return type from Dog class constructor and destructor, change nonexistant printDog function to print. --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 9f8f5f32..2c6d51a9 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -300,7 +300,7 @@ public: }; // A semicolon must follow the class definition. // Class member functions are usually implemented in .cpp files. -void Dog::Dog() +Dog::Dog() { std::cout << "A dog has been constructed\n"; } @@ -323,7 +323,7 @@ void Dog::print() const std::cout << "Dog is " << name << " and weighs " << weight << "kg\n"; } -void Dog::~Dog() +Dog::~Dog() { cout << "Goodbye " << name << "\n"; } @@ -332,7 +332,7 @@ int main() { Dog myDog; // prints "A dog has been constructed" myDog.setName("Barkley"); myDog.setWeight(10); - myDog.printDog(); // prints "Dog is Barkley and weighs 10 kg" + myDog.print(); // prints "Dog is Barkley and weighs 10 kg" return 0; } // prints "Goodbye Barkley" -- cgit v1.2.3 From 3db1042157204ad05484d6b42140261f849040cc Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 31 May 2015 21:42:03 -0400 Subject: Add missing semicolons. --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 2c6d51a9..5cd491b9 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -288,7 +288,7 @@ public: // Functions can also be defined inside the class body. // Functions defined as such are automatically inlined. - void bark() const { std::cout << name << " barks!\n" } + void bark() const { std::cout << name << " barks!\n"; } // Along with constructors, C++ provides destructors. // These are called when an object is deleted or falls out of scope. @@ -341,7 +341,7 @@ int main() { // This class inherits everything public and protected from the Dog class class OwnedDog : public Dog { - void setOwner(const std::string& dogsOwner) + void setOwner(const std::string& dogsOwner); // Override the behavior of the print function for all OwnedDogs. See // http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping -- cgit v1.2.3 From cea52ca43490b74316781c23779654fd46aaeab4 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 1 Jun 2015 21:57:18 -0400 Subject: Exceptions do not take a string argument in their constructor. --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 5cd491b9..b214de7a 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -523,7 +523,7 @@ printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" // _catch_ handlers. try { // Do not allocate exceptions on the heap using _new_. - throw std::exception("A problem occurred"); + throw std::exception(); } // Catch exceptions by const reference if they are objects catch (const std::exception& ex) @@ -614,7 +614,7 @@ void doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); // Open the file in read mode if (fh == nullptr) - throw std::exception("Could not open the file."); + throw std::exception(); try { doSomethingWithTheFile(fh); -- cgit v1.2.3 From 47d3cea47e8c5203efa857070a00dcfbff67b019 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 1 Jun 2015 22:00:52 -0400 Subject: Template example class should have public method so it can get called externally. --- c++.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index b214de7a..d049408a 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -445,6 +445,7 @@ int main () { // define a class or function that takes a type parameter: template class Box { +public: // In this class, T can be used as any other type. void insert(const T&) { ... } }; -- cgit v1.2.3 From 894792e1e17173823a5d50de24439427c69d63f4 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 2 Jun 2015 16:30:35 -0400 Subject: Using std::runtime_error instead of std::exception. --- c++.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index d049408a..6f4d2959 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -519,12 +519,13 @@ printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" // (see http://en.cppreference.com/w/cpp/error/exception) // but any type can be thrown an as exception #include +#include // All exceptions thrown inside the _try_ block can be caught by subsequent // _catch_ handlers. try { // Do not allocate exceptions on the heap using _new_. - throw std::exception(); + throw std::runtime_error("A problem occurred"); } // Catch exceptions by const reference if they are objects catch (const std::exception& ex) -- cgit v1.2.3 From 06889be239622266d9c36c750f7ee755ccdae05d Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 2 Jun 2015 19:14:52 -0400 Subject: Updated other exception to also be runtime_error type instead. --- 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 6f4d2959..ff2a98fd 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -616,7 +616,7 @@ void doSomethingWithAFile(const char* filename) { FILE* fh = fopen(filename, "r"); // Open the file in read mode if (fh == nullptr) - throw std::exception(); + throw std::runtime_error("Could not open the file."); try { doSomethingWithTheFile(fh); -- cgit v1.2.3