From 5f52fe22641b0361bf8e27f56c478bbabba18618 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sat, 19 Apr 2025 19:27:00 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?= =?UTF-8?q?=20when=20const=20shit=20is=20passed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex01/iter.hpp | 10 ++++++++-- ex01/main.cpp | 10 +++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ex01/iter.hpp b/ex01/iter.hpp index 1e19c75..ae323f2 100644 --- a/ex01/iter.hpp +++ b/ex01/iter.hpp @@ -6,13 +6,19 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 14:56:23 by adjoly #+# #+# */ -/* Updated: 2025/04/09 15:23:21 by adjoly ### ########.fr */ +/* Updated: 2025/04/19 19:25:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -template void iter(T *arr, int len, void(func)(T &)) { +template void iter(T *arr, int len, void(*func)(T &)) { + for (int i = 0; i< len; i++) { + func(arr[i]); + } +} + +template void iter(const T *arr, int len, void(*func)(const T &)) { for (int i = 0; i< len; i++) { func(arr[i]); } diff --git a/ex01/main.cpp b/ex01/main.cpp index efde849..8456a3d 100644 --- a/ex01/main.cpp +++ b/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 15:17:59 by adjoly #+# #+# */ -/* Updated: 2025/04/09 15:25:40 by adjoly ### ########.fr */ +/* Updated: 2025/04/19 19:26:24 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,15 @@ void increment(int &nb) { nb++; } -void printInt(int &nb) { +void printInt(const int &nb) { std::cout << nb << std::endl; } int main(void) { int arr[4] = {1, 2, 3, 4}; - iter(arr, 4, printInt); + iter(arr, 4, &printInt); std::cout << "incrementing" << std::endl; - iter(arr, 4, increment); - iter(arr, 4, printInt); + iter(arr, 4, &increment); + iter(arr, 4, &printInt); }