From 24943a4282302ac6067eaaec2ee968c427be2c69 Mon Sep 17 00:00:00 2001 From: Sasza Stanczew Date: Fri, 26 Apr 2024 15:46:09 +0200 Subject: [PATCH] =?UTF-8?q?AdvC++=20-=20r=C3=B3=C5=BCnica=20mi=C4=99dfzy?= =?UTF-8?q?=20star=C4=85=20alokacj=C4=85=20a=20smart=20pointerami?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exercises/resource/CMakeLists.txt | 11 ++++++- .../exercises/resource/resource.cpp | 1 + .../exercises/resource/resource2.cpp | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 AdvancedCppV2/Presentation/exercises/resource/resource2.cpp diff --git a/AdvancedCppV2/Presentation/exercises/resource/CMakeLists.txt b/AdvancedCppV2/Presentation/exercises/resource/CMakeLists.txt index 78dd153..7218c5a 100644 --- a/AdvancedCppV2/Presentation/exercises/resource/CMakeLists.txt +++ b/AdvancedCppV2/Presentation/exercises/resource/CMakeLists.txt @@ -11,4 +11,13 @@ set(SRC_LIST add_executable(${PROJECT_NAME} ${SRC_LIST}) target_compile_options(${PROJECT_NAME} PUBLIC -Wall -Werror -Wpedantic -Wextra) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}) \ No newline at end of file +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}) +project(Resource2) + +set(SRC_LIST + resource2.cpp +) + +add_executable(${PROJECT_NAME} ${SRC_LIST}) +target_compile_options(${PROJECT_NAME} PUBLIC -Wall -Werror -Wpedantic -Wextra) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}) diff --git a/AdvancedCppV2/Presentation/exercises/resource/resource.cpp b/AdvancedCppV2/Presentation/exercises/resource/resource.cpp index e0c201a..5aba38e 100644 --- a/AdvancedCppV2/Presentation/exercises/resource/resource.cpp +++ b/AdvancedCppV2/Presentation/exercises/resource/resource.cpp @@ -27,6 +27,7 @@ int main(int argc, char* argv[]) { delete rsc; } catch (std::logic_error& e) { std::cout << e.what() << '\n'; + // wyciek pamięci bo nie ma delete } return 0; } diff --git a/AdvancedCppV2/Presentation/exercises/resource/resource2.cpp b/AdvancedCppV2/Presentation/exercises/resource/resource2.cpp new file mode 100644 index 0000000..6531fe3 --- /dev/null +++ b/AdvancedCppV2/Presentation/exercises/resource/resource2.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +class Resource { +public: + void use(const char* N) { + std::cout << "Using resource. Passed " << *N << '\n'; + if (*N == 'd') { + throw std::logic_error("Passed d. d is prohibited."); + } + }; +}; + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "You need to pass 1 argument" << '\n'; + exit(-1); + } + + const char* arg = argv[1]; + std::unique_ptr rsc = nullptr; + + try { + rsc = std::make_unique(); + rsc->use(arg); + } catch (std::logic_error& e) { + std::cout << e.what() << '\n'; + } + return 0; +}