Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Nov 5, 2014
1 parent 7204e30 commit b99b3c2
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion ch06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,40 @@ int calc(const char*, const char*); calls lookup(const char *)

illegal. both calls lookup(char*)

## [Exercise 6.54 ~ 6.56](ex6_54_55_56_functionPointer.cpp)
## Exercise 6.54
```cpp
int func(int a, int b);

using pFunc1 = decltype(func) *;
typedef decltype(func) *pFunc2;
using pFunc3 = int (*)(int a, int b);
using pFunc4 = int(int a, int b);
typedef int(*pFunc5)(int a, int b);
using pFunc6 = decltype(func);

std::vector<pFunc1> vec1;
std::vector<pFunc2> vec2;
std::vector<pFunc3> vec3;
std::vector<pFunc4*> vec4;
std::vector<pFunc5> vec5;
std::vector<pFunc6*> vec6;
```
## Exercise 6.55
```cpp
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
```

## Exercise 6.56
```cpp
std::vector<decltype(func) *> vec{add, subtract, multiply, divide};
for (auto f : vec)
std::cout << f(2, 2) << std::endl;
```
----
see @Mooophy 's [complete codes](ex6_54_55_56.cpp).

0 comments on commit b99b3c2

Please sign in to comment.