Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pezy/Cpp-Primer
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Feb 10, 2015
2 parents 6cbc78d + e46d946 commit 9b7bc0a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 44 deletions.
11 changes: 8 additions & 3 deletions ch02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ int month = 9, day = 7;
int month = 09, day = 07;
```

The first line's integer is decimal, the second line's integer is octal.
The first line's integer is decimal.

The second line:

1. `int month = 09` is invalid, cause octal don't have digit `9`.
2. `day` is octal.

##Exercise 2.7
> What values do these literals represent? What type does each
Expand Down Expand Up @@ -158,7 +163,7 @@ int input_value = 0;
std::cin >> input_value;
```

(b):---when you compile the code without the argument "-std=c++11", you will get the warning below:
(b):---when you compile the code without the argument "-std=c++11", you will get the warning below:
warning: implicit conversion from 'double' to 'int' changes value from 3.14 to 3.
---when you compile the code using "-std=c+11", you will get a error below:
error: type 'double' cannot be narrowed to 'int' in initializer list
Expand Down Expand Up @@ -195,7 +200,7 @@ int main()
`global_int` is global variable, so the value is zero.
`local_int` is a local variable which is not uninitialized, so it has a undefined value.
`local_str` is also a local variable which is not uninitialized, but it has a value that is defined by the class. So it is empty string.
PS: please read P44 in the English version, P40 in Chinese version to get more.
PS: please read P44 in the English version, P40 in Chinese version to get more.
The note: Uninitialized objects of built-in type defined inside a function body have a undifined value. Objects of class type that we do not explicitly inititalize have a value that is defined by class.

##Exercise 2.11
Expand Down
37 changes: 37 additions & 0 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,40 @@ for_each(elements, first_free, [this](std::string &rhs){ alloc.destroy(&rhs); })
The new version is better. Compared to the old one, it doesn't need to worry about the order and decrement.So more straightforward and handy. The only thing to do for using this approach is to add "&" to build the pointers to string pointers.

## Exercise 13.44 [hpp](ex13_44.h) | [cpp](ex13_44.cpp) | [Test](ex13_44_TEST.cpp)

## Exercise 13.45:
>Distinguish between an rvalue reference and an lvalue reference.
Definition:

- lvalue reference: reference that can bind to **an lvalue**. (Regular reference)
- rvalue reference: reference **to an object that is about to be destroyed**.

We can bind an rvalue reference to expression that require conversion, to literals, or to expressions that return an rvalue, but we cannot directly bind an rvalue reference to an lvalue.

```cpp
int i = 42;
int &r = i; // lvalue reference
int &&rr = i; // rvalue reference (Error: i is a lvalue)
int &r2 = i*42; // lvalue reference (Error: i*42 is a rvalue)
const int &r3 = i*42; // reference to const (bind to a rvalue)
int &&rr2 = i*42; // rvalue reference
```

- lvalue : functions that return lvalue references, assignment, subscript, dereference, and prefix increment/decrement operator.
- rvalue / const reference : functions that return a nonreferences, arithmetic, relational bitwise, postfix increment/decrement operators.

## Exercise 13.46:
>Which kind of reference can be bound to the following initializers?
```cpp
int f();
vector<int> vi(100);
int&& r1 = f();
int& r2 = vi[0];
int& r3 = r1;
int&& r4 = vi[0] * f();
```
## Exercise 13.47 [hpp](ex13_47.h) | [cpp](ex13_47.cpp)
## [Exercise 13.48](ex13_48.cpp)
39 changes: 0 additions & 39 deletions ch13/ex13.45.46/main.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion ch13/ex13.47.48/string.cpp → ch13/ex13_47.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @remark
***************************************************************************/

#include "string.h"
#include "ex13_47.h"
#include <algorithm>
#include <iostream>

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ch13/ex13.47.48/main.cpp → ch13/ex13_48.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// std::vector<T> performs reallocation by calling String's copy constructor.
// In a word, it depends on the state of std::vector<String>.
//!
#include "string.h"
#include "ex13_47.h"
#include <vector>

int main()
Expand Down

0 comments on commit 9b7bc0a

Please sign in to comment.