Skip to content

Commit

Permalink
add ex2.33~2.35
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Aug 14, 2014
1 parent 383feb8 commit fe540a7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ch02/ex2_34.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

int main()
{
int i = 0, &r = i;
auto a = r; // a is an int (r is an alias for i, which has type int)

const int ci = i, &cr = ci;
auto b = ci; // b is an int (top-level const in ci is dropped)
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
auto d = &i; // d is an int* (& ofan int objectis int*)
auto e = &ci; // e is const int*(& of a const object is low-level const)

const auto f = ci; // deduced type of ci is int; f has type const int
auto &g = ci; // g is a const int& that is bound to ci

a=42; b=42; c=42; *d=42; e=&c;

return 0;
}
21 changes: 21 additions & 0 deletions ch02/ex2_35.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <typeinfo>

int main()
{
const int i = 42;
auto j = i;
const auto &k = i;
auto *p = &i;
const auto j2 = i, &k2 = i;

// print i means int, and PKi means pointer to const int.
std::cout << "j is " << typeid(j).name()
<< "\nk is " << typeid(k).name()
<< "\np is " << typeid(p).name()
<< "\nj2 is " << typeid(j2).name()
<< "\nk2 is " << typeid(k2).name()
<< std::endl;

return 0;
}
1 change: 1 addition & 0 deletions docs/ch02/ex2.33_2.35.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
##Exercise 2.33>Using the variable definitions from this section, determinewhat happens in each of these assignments:```cppa=42; // set 42 to int a.b=42; // set 42 to int b.c=42; // set 42 to int c.d=42; // ERROR, d is an int *. correct: *d = 42;e=42; // ERROR, e is an const int *. correct: e = &c;g=42; // ERROR, g is a const int& that is bound to ci.```##Exercise 2.34>Write a program containing the variables and assignments from theprevious exercise.Print the variables before and after the assignments to checkwhether your predictions in the previous exercise were correct.If not, study the examples until you can convince yourself you knowwhat led you to the wrong conclusion.[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_34.cpp) is the code.##Exercise 2.35>Determine the types deduced in each of the following definitions.Once you’ve figured out the types,write a program to see whether you were correct.```cppconst int i = 42;auto j = i; const auto &k = i; auto *p = &i; const auto j2 = i, &k2 = i;```j and j2 are int.k and k2 are &int.p is a pointer to const int.[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_35.cpp) is the code.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ C++ Primer 5th Answer Note
- [Exercise 2.9 ~ 2.14](/Cpp-Primer/ch02/ex2.9_2.14)
- [Exercise 2.15 ~ 2.25](/Cpp-Primer/ch02/ex2.15_2.25)
- [Exercise 2.26 ~ 2.32](/Cpp-Primer/ch02/ex2.26_2.32)
- [Exercise 2.33 ~ 2.35](/Cpp-Primer/ch02/ex2.33_2.35)
- Chapter 3. Strings, Vectors, and Arrays
- Chapter 4. Expressions
- Chapter 5. Statements
Expand Down
3 changes: 3 additions & 0 deletions docs/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ <h1 id="gistid"><a href="http://github.com/{{USER}}/{{NAME}}">{{NAME}}</a> :: <a
ga('create', 'UA-6824126-17', 'viewdocs.io');
ga('send', 'pageview');
</script>
<a href="https://github.com/pezy/Cpp-primer#c-primer-5th-edition-exercise-answers">
<img style="position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/567c3a48d796e2fc06ea80409cc9dd82bf714434/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png">
</a>
<script src="//static.getclicky.com/js" type="text/javascript"></script>
<script type="text/javascript">try{ clicky.init(100672863); }catch(e){}</script>
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/100672863ns.gif" /></p></noscript>
Expand Down

0 comments on commit fe540a7

Please sign in to comment.