Skip to content

Commit

Permalink
Update ex6_54_55_56.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy authored and pezy committed Jun 24, 2015
1 parent a0fea7b commit 5b3cd0a
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions ch06/ex6_54_55_56.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! @Alan
//! @Yue Wang
//!
//! Exercise 6.54:
//! Write a declaration for a function that takes two int
Expand All @@ -17,9 +17,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using namespace std;
using std::vector; using std::cout;

//!
//! @brief Exercise 6.54
Expand All @@ -32,25 +30,21 @@ typedef decltype(f) fp;//fp is just a function type not a function pointer
//! @brief Exercise 6.55
//! @note Store pointers to these functions in the vector
//!
inline int NumAdd(const int n1, const int n2) {return n1+n2;}
inline int NumSub(const int n1, const int n2) {return n1-n2;}
inline int NumMul(const int n1, const int n2) {return n1*n2;}
inline int NumDiv(const int n1, const int n2) {return n1/n2;}

vector<fp*> v{NumAdd, NumSub, NumMul, NumDiv};
inline int NumAdd(const int n1, const int n2) { return n1 + n2; }
inline int NumSub(const int n1, const int n2) { return n1 - n2; }
inline int NumMul(const int n1, const int n2) { return n1 * n2; }
inline int NumDiv(const int n1, const int n2) { return n1 / n2; }

vector<fp*> v{ NumAdd, NumSub, NumMul, NumDiv };

int main()
{
//!
//! @brief Exercise 6.56
//! @note Call each element in the vector and print their result.
//!
for(vector<fp*>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << (*it)(2,2) // here shows how to use it!
<< std::endl;
}
for (auto it = v.cbegin(); it != v.cend(); ++it)
cout << (*it)(2, 2) << std::endl;

return 0;
}

0 comments on commit 5b3cd0a

Please sign in to comment.