Skip to content

Commit cc7d385

Browse files
committed
Refactor identify function to use dynamic_cast for better type checking and remove unnecessary output
1 parent 7b47057 commit cc7d385

5 files changed

Lines changed: 23 additions & 29 deletions

File tree

06/ex02/Base.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,24 @@ void identify(Base* p) {
4141

4242
void identify(Base& p) {
4343
try {
44-
if (dynamic_cast<A*>(&p)) {
45-
std::cout << "A" << std::endl;
46-
} else if (dynamic_cast<B*>(&p)) {
47-
std::cout << "B" << std::endl;
48-
} else if (dynamic_cast<C*>(&p)) {
49-
std::cout << "C" << std::endl;
50-
} else {
51-
std::cout << "Unknown type" << std::endl;
52-
}
44+
A& a = dynamic_cast<A&>(p);
45+
std::cout << "A" << std::endl;
46+
(void)a; // To avoid unused variable warning
47+
return;
48+
} catch (const std::exception& e) {
49+
}
50+
try {
51+
B& b = dynamic_cast<B&>(p);
52+
std::cout << "B" << std::endl;
53+
(void)b; // To avoid unused variable warning
54+
return;
55+
} catch (const std::exception& e) {
56+
}
57+
try {
58+
C& c = dynamic_cast<C&>(p);
59+
std::cout << "C" << std::endl;
60+
(void)c; // To avoid unused variable warning
61+
return;
5362
} catch (const std::exception& e) {
54-
std::cout << "Exception: " << e.what() << std::endl;
5563
}
5664
}

08/ex00/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ format:
2727
@clang-format -i $(SRC) $(HEADERS)
2828

2929
run: all
30-
@printf "\n🤖 07/ex00 $(NAME) output:\n\n"
30+
@printf "\n🤖 08/ex00 $(NAME) output:\n\n"
3131
@./$(NAME)
3232

3333
.PHONY: all clean fclean re

08/ex01/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ format:
2727
@clang-format -i $(SRC) $(HEADERS)
2828

2929
run: all
30-
@printf "\n🤖 07/ex00 $(NAME) output:\n\n"
30+
@printf "\n🤖 08/ex01 $(NAME) output:\n\n"
3131
@./$(NAME)
3232

3333
.PHONY: all clean fclean re

08/ex02/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ format:
2727
@clang-format -i $(SRC) $(HEADERS)
2828

2929
run: all
30-
@printf "\n🤖 07/ex00 $(NAME) output:\n\n"
30+
@printf "\n🤖 08/ex02 $(NAME) output:\n\n"
3131
@./$(NAME)
3232

3333
.PHONY: all clean fclean re

08/ex02/MutantStack.hpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,12 @@ class MutantStack : public std::stack<T, Container> {
1919
~MutantStack() = default;
2020

2121
iterator begin() { return this->c.begin(); }
22-
2322
iterator end() { return this->c.end(); }
24-
25-
const_iterator begin() const { return this->c.begin(); }
26-
27-
const_iterator end() const { return this->c.end(); }
28-
29-
const_iterator cbegin() const { return this->c.cbegin(); }
30-
31-
const_iterator cend() const { return this->c.cend(); }
32-
3323
reverse_iterator rbegin() { return this->c.rbegin(); }
34-
3524
reverse_iterator rend() { return this->c.rend(); }
3625

26+
const_iterator begin() const { return this->c.begin(); }
27+
const_iterator end() const { return this->c.end(); }
3728
const_reverse_iterator rbegin() const { return this->c.rbegin(); }
38-
3929
const_reverse_iterator rend() const { return this->c.rend(); }
40-
41-
const_reverse_iterator crbegin() const { return this->c.crbegin(); }
42-
43-
const_reverse_iterator crend() const { return this->c.crend(); }
4430
};

0 commit comments

Comments
 (0)