mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-18 20:30:58 +00:00
Misc. typos and formatting
This commit is contained in:
parent
455afa3a7b
commit
12286a4b78
@ -159,9 +159,9 @@ void foo()
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Includes all symbols from `namesapce Second` into the current scope. Note
|
// Includes all symbols from namespace Second into the current scope. Note
|
||||||
// that simply `foo()` no longer works, since it is now ambiguous whether
|
// that simply foo() no longer works, since it is now ambiguous whether
|
||||||
// we're calling the `foo` in `namespace Second` or the top level.
|
// we're calling the foo in namespace Second or the top level.
|
||||||
using namespace Second;
|
using namespace Second;
|
||||||
|
|
||||||
Second::foo(); // prints "This is Second::foo"
|
Second::foo(); // prints "This is Second::foo"
|
||||||
@ -256,7 +256,7 @@ string tempObjectFun() { ... }
|
|||||||
string retVal = tempObjectFun();
|
string retVal = tempObjectFun();
|
||||||
|
|
||||||
// What happens in the second line is actually:
|
// What happens in the second line is actually:
|
||||||
// - a string object is returned from `tempObjectFun`
|
// - a string object is returned from tempObjectFun
|
||||||
// - a new string is constructed with the returned object as arugment to the
|
// - a new string is constructed with the returned object as arugment to the
|
||||||
// constructor
|
// constructor
|
||||||
// - the returned object is destroyed
|
// - the returned object is destroyed
|
||||||
@ -268,15 +268,15 @@ string retVal = tempObjectFun();
|
|||||||
// code:
|
// code:
|
||||||
foo(bar(tempObjectFun()))
|
foo(bar(tempObjectFun()))
|
||||||
|
|
||||||
// assuming `foo` and `bar` exist, the object returned from `tempObjectFun` is
|
// assuming foo and bar exist, the object returned from tempObjectFun is
|
||||||
// passed to `bar`, and it is destroyed before `foo` is called.
|
// passed to bar, and it is destroyed before foo is called.
|
||||||
|
|
||||||
// Now back to references. The exception to the "at the end of the enclosing
|
// Now back to references. The exception to the "at the end of the enclosing
|
||||||
// expression" rule is if a temporary object is bound to a const reference, in
|
// expression" rule is if a temporary object is bound to a const reference, in
|
||||||
// which case its life gets extended to the current scope:
|
// which case its life gets extended to the current scope:
|
||||||
|
|
||||||
void constReferenceTempObjectFun() {
|
void constReferenceTempObjectFun() {
|
||||||
// `constRef` gets the temporary object, and it is valid until the end of this
|
// constRef gets the temporary object, and it is valid until the end of this
|
||||||
// function.
|
// function.
|
||||||
const string& constRef = tempObjectFun();
|
const string& constRef = tempObjectFun();
|
||||||
...
|
...
|
||||||
@ -301,7 +301,7 @@ basic_string(basic_string&& other);
|
|||||||
// Idea being if we are constructing a new string from a temporary object (which
|
// Idea being if we are constructing a new string from a temporary object (which
|
||||||
// is going to be destroyed soon anyway), we can have a more efficient
|
// is going to be destroyed soon anyway), we can have a more efficient
|
||||||
// constructor that "salvages" parts of that temporary string. You will see this
|
// constructor that "salvages" parts of that temporary string. You will see this
|
||||||
// concept referred to as the move semantic.
|
// concept referred to as "move semantics".
|
||||||
|
|
||||||
//////////////////////////////////////////
|
//////////////////////////////////////////
|
||||||
// Classes and object-oriented programming
|
// Classes and object-oriented programming
|
||||||
@ -349,10 +349,10 @@ public:
|
|||||||
// These are called when an object is deleted or falls out of scope.
|
// These are called when an object is deleted or falls out of scope.
|
||||||
// This enables powerful paradigms such as RAII
|
// This enables powerful paradigms such as RAII
|
||||||
// (see below)
|
// (see below)
|
||||||
// Destructors should be virtual if a class is to be derived from;
|
// The destructor should be virtual if a class is to be derived from;
|
||||||
// if they are not virtual, then any resources allocated using RAII in
|
// if it is not virtual, then the derived class' destructor will
|
||||||
// the derived class will not be released if it destroyed through a
|
// not be called if the object is destroyed through a base-class reference
|
||||||
// base-class reference or pointer.
|
// or pointer.
|
||||||
virtual ~Dog();
|
virtual ~Dog();
|
||||||
|
|
||||||
}; // A semicolon must follow the class definition.
|
}; // A semicolon must follow the class definition.
|
||||||
@ -495,9 +495,10 @@ int main () {
|
|||||||
/////////////////////
|
/////////////////////
|
||||||
|
|
||||||
// Templates in C++ are mostly used for generic programming, though they are
|
// Templates in C++ are mostly used for generic programming, though they are
|
||||||
// much more powerful than generics constructs in other languages. It also
|
// much more powerful than generic constructs in other languages. They also
|
||||||
// supports explicit and partial specialization, functional-style type classes,
|
// support explicit and partial specialization and functional-style type
|
||||||
// and also it's Turing-complete.
|
// classes; in fact, they are a Turing-complete functional language embedded
|
||||||
|
// in C++!
|
||||||
|
|
||||||
// We start with the kind of generic programming you might be familiar with. To
|
// We start with the kind of generic programming you might be familiar with. To
|
||||||
// define a class or function that takes a type parameter:
|
// define a class or function that takes a type parameter:
|
||||||
@ -509,7 +510,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// During compilation, the compiler actually generates copies of each template
|
// During compilation, the compiler actually generates copies of each template
|
||||||
// with parameters substituted, and so the full definition of the class must be
|
// with parameters substituted, so the full definition of the class must be
|
||||||
// present at each invocation. This is why you will see template classes defined
|
// present at each invocation. This is why you will see template classes defined
|
||||||
// entirely in header files.
|
// entirely in header files.
|
||||||
|
|
||||||
@ -523,13 +524,13 @@ intBox.insert(123);
|
|||||||
Box<Box<int> > boxOfBox;
|
Box<Box<int> > boxOfBox;
|
||||||
boxOfBox.insert(intBox);
|
boxOfBox.insert(intBox);
|
||||||
|
|
||||||
// Up until C++11, you must place a space between the two '>'s, otherwise '>>'
|
// Until C++11, you had to place a space between the two '>'s, otherwise '>>'
|
||||||
// will be parsed as the right shift operator.
|
// would be parsed as the right shift operator.
|
||||||
|
|
||||||
// You will sometimes see
|
// You will sometimes see
|
||||||
// template<typename T>
|
// template<typename T>
|
||||||
// instead. The 'class' keyword and 'typename' keyword are _mostly_
|
// instead. The 'class' keyword and 'typename' keywords are _mostly_
|
||||||
// interchangeable in this case. For full explanation, see
|
// interchangeable in this case. For the full explanation, see
|
||||||
// http://en.wikipedia.org/wiki/Typename
|
// http://en.wikipedia.org/wiki/Typename
|
||||||
// (yes, that keyword has its own Wikipedia page).
|
// (yes, that keyword has its own Wikipedia page).
|
||||||
|
|
||||||
@ -585,12 +586,15 @@ try {
|
|||||||
// Do not allocate exceptions on the heap using _new_.
|
// Do not allocate exceptions on the heap using _new_.
|
||||||
throw std::runtime_error("A problem occurred");
|
throw std::runtime_error("A problem occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Catch exceptions by const reference if they are objects
|
// Catch exceptions by const reference if they are objects
|
||||||
catch (const std::exception& ex)
|
catch (const std::exception& ex)
|
||||||
{
|
{
|
||||||
std::cout << ex.what();
|
std::cout << ex.what();
|
||||||
|
}
|
||||||
|
|
||||||
// Catches any exception not caught by previous _catch_ blocks
|
// Catches any exception not caught by previous _catch_ blocks
|
||||||
} catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
std::cout << "Unknown exception caught";
|
std::cout << "Unknown exception caught";
|
||||||
throw; // Re-throws the exception
|
throw; // Re-throws the exception
|
||||||
@ -600,8 +604,8 @@ catch (const std::exception& ex)
|
|||||||
// RAII
|
// RAII
|
||||||
///////
|
///////
|
||||||
|
|
||||||
// RAII stands for Resource Allocation Is Initialization.
|
// RAII stands for "Resource Acquisition Is Initialization".
|
||||||
// It is often considered the most powerful paradigm in C++,
|
// It is often considered the most powerful paradigm in C++
|
||||||
// and is the simple concept that a constructor for an object
|
// and is the simple concept that a constructor for an object
|
||||||
// acquires that object's resources and the destructor releases them.
|
// acquires that object's resources and the destructor releases them.
|
||||||
|
|
||||||
@ -752,7 +756,9 @@ int* pt2 = new int;
|
|||||||
*pt2 = nullptr; // Doesn't compile
|
*pt2 = nullptr; // Doesn't compile
|
||||||
pt2 = nullptr; // Sets pt2 to null.
|
pt2 = nullptr; // Sets pt2 to null.
|
||||||
|
|
||||||
// But somehow 'bool' type is an exception (this is to make `if (ptr)` compile).
|
// There is an exception made for bools.
|
||||||
|
// This is to allow you to test for null pointers with if(!ptr),
|
||||||
|
// but as a consequence you can assign nullptr to a bool directly!
|
||||||
*pt = nullptr; // This still compiles, even though '*pt' is a bool!
|
*pt = nullptr; // This still compiles, even though '*pt' is a bool!
|
||||||
|
|
||||||
|
|
||||||
@ -779,12 +785,12 @@ vector<Foo> v;
|
|||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
v.push_back(Foo());
|
v.push_back(Foo());
|
||||||
|
|
||||||
// Following line sets size of v to 0, but destructors don't get called,
|
// Following line sets size of v to 0, but destructors don't get called
|
||||||
// and resources aren't released!
|
// and resources aren't released!
|
||||||
v.empty();
|
v.empty();
|
||||||
v.push_back(Foo()); // New value is copied into the first Foo we inserted in the loop.
|
v.push_back(Foo()); // New value is copied into the first Foo we inserted
|
||||||
|
|
||||||
// Truly destroys all values in v. See section about temporary object for
|
// Truly destroys all values in v. See section about temporary objects for
|
||||||
// explanation of why this works.
|
// explanation of why this works.
|
||||||
v.swap(vector<Foo>());
|
v.swap(vector<Foo>());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user