diff options
Diffstat (limited to 'cpp07/ex01')
| -rw-r--r-- | cpp07/ex01/iter.hpp | 35 | ||||
| -rw-r--r-- | cpp07/ex01/main.cpp | 40 |
2 files changed, 73 insertions, 2 deletions
diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp index df1a37a..8118060 100644 --- a/cpp07/ex01/iter.hpp +++ b/cpp07/ex01/iter.hpp @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/14 15:06:48 by cacharle #+# #+# */ -/* Updated: 2020/12/14 15:18:09 by cacharle ### ########.fr */ +/* Updated: 2020/12/15 11:23:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,42 @@ void iter(T* ptr, size_t len, void (*f)(T const& x)) } template<typename T> +void iter(T* ptr, size_t len, void (*f)(T& x)) +{ + for (size_t i = 0; i < len; i++) + f(ptr[i]); +} + +template<typename T> +void iter(T* ptr, size_t len, void (*f)(T x)) +{ + for (size_t i = 0; i < len; i++) + f(ptr[i]); +} + +template<typename T> void timeTwo(T const& x) { std::cout << "timeTwo function " << x << " * 2 = " << x * 2 << std::endl; } +template<typename T> +void timeThree(T& x) +{ + std::cout << "timeThree function " << x << " * 3 = " << x * 3 << std::endl; +} + +class NotAnInt +{ +public: + NotAnInt(); + NotAnInt(int n); + NotAnInt(NotAnInt const& other); + NotAnInt& operator=(NotAnInt const& other); + int getN() const; + +private: + int m_n; +}; + #endif diff --git a/cpp07/ex01/main.cpp b/cpp07/ex01/main.cpp index 6ac8e82..3815f77 100644 --- a/cpp07/ex01/main.cpp +++ b/cpp07/ex01/main.cpp @@ -6,13 +6,29 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 20:02:19 by charles #+# #+# */ -/* Updated: 2020/12/14 15:16:32 by cacharle ### ########.fr */ +/* Updated: 2020/12/15 11:26:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <iostream> #include "iter.hpp" +NotAnInt::NotAnInt() : m_n(0) {} +NotAnInt::NotAnInt(int n) : m_n(n) {} +NotAnInt::NotAnInt(NotAnInt const& other) : m_n(other.m_n) {} +NotAnInt& NotAnInt::operator=(NotAnInt const& other) { m_n = other.m_n; return *this; } +int NotAnInt::getN() const { return m_n; } + +void intOnly(int n) +{ + std::cout << "intOnly() " << n << std::endl; +} + +void notAnIntOnly(NotAnInt& n) +{ + std::cout << "notAnIntOnly() " << n.getN() << std::endl; +} + int main() { int intArray[] = {1, 2, 3, 4, 10, 20, 30, -1, -2}; @@ -21,6 +37,8 @@ int main() std::cout << intArray[i] << ", "; std::cout << std::endl; iter(intArray, intArraySize, timeTwo<int>); + iter(intArray, intArraySize, timeThree<int>); + iter(intArray, intArraySize, intOnly); std::cout << "--------------------------------------" << std::endl; @@ -30,6 +48,7 @@ int main() std::cout << floatArray[i] << ", "; std::cout << std::endl; iter(floatArray, floatArraySize, timeTwo<float>); + iter(floatArray, floatArraySize, timeThree<float>); std::cout << "--------------------------------------" << std::endl; @@ -39,6 +58,25 @@ int main() std::cout << uintArray[i] << ", "; std::cout << std::endl; iter(uintArray, uintArraySize, timeTwo<unsigned int>); + iter(uintArray, uintArraySize, timeThree<unsigned int>); + std::cout << "--------------------------------------" << std::endl; + + const int cintArray[] = {1, 2, 3, 4}; + size_t cintArraySize = sizeof(cintArray) / sizeof(const int); + for (size_t i = 0; i < cintArraySize; i++) + std::cout << cintArray[i] << ", "; + std::cout << std::endl; + iter(cintArray, cintArraySize, timeTwo<const int>); + iter(cintArray, cintArraySize, timeThree<const int>); + + std::cout << "--------------------------------------" << std::endl; + + NotAnInt nintArray[] = {NotAnInt(1), NotAnInt(2), NotAnInt(3), NotAnInt(4)}; + size_t nintArraySize = sizeof(nintArray) / sizeof(NotAnInt); + for (size_t i = 0; i < nintArraySize; i++) + std::cout << nintArray[i].getN() << ", "; + std::cout << std::endl; + iter(nintArray, nintArraySize, notAnIntOnly); return 0; } |
