aboutsummaryrefslogtreecommitdiff
path: root/cpp06/ex02/main.cpp
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-11-18 10:30:31 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-11-18 10:30:31 +0100
commit6169d697a5be59426d034b878bffc848de49491d (patch)
treeea339e67a4814a04ada4c5c0a1c3cdfc2bc7b6fa /cpp06/ex02/main.cpp
parent452e6bfa7bb4bca75dc4659bf9d707101b411977 (diff)
downloadpiscine_cpp-6169d697a5be59426d034b878bffc848de49491d.tar.gz
piscine_cpp-6169d697a5be59426d034b878bffc848de49491d.tar.bz2
piscine_cpp-6169d697a5be59426d034b878bffc848de49491d.zip
Added cpp06/ex01 and ex02
Diffstat (limited to 'cpp06/ex02/main.cpp')
-rw-r--r--cpp06/ex02/main.cpp92
1 files changed, 90 insertions, 2 deletions
diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp
index 17c0c3d..86cd78f 100644
--- a/cpp06/ex02/main.cpp
+++ b/cpp06/ex02/main.cpp
@@ -6,12 +6,14 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 10:00:18 by charles #+# #+# */
-/* Updated: 2020/04/15 10:05:23 by charles ### ########.fr */
+/* Updated: 2020/11/18 09:29:49 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
+#include <fstream>
#include <cstdlib>
+#include <ctime>
#include "Base.hpp"
#include "A.hpp"
#include "B.hpp"
@@ -28,9 +30,95 @@ Base* generate(void)
return NULL;
}
+void identify_from_pointer(Base *p)
+{
+ if (dynamic_cast<A*>(p) != NULL)
+ std::cout << "A" << std::endl;
+ else if (dynamic_cast<B*>(p) != NULL)
+ std::cout << "B" << std::endl;
+ else if (dynamic_cast<C*>(p) != NULL)
+ std::cout << "C" << std::endl;
+ else
+ std::cout << "Couldn't identify from pointer" << std::endl;
+}
+
+void identify_from_reference(Base &p)
+{
+ try
+ {
+ (void)dynamic_cast<A&>(p);
+ std::cout << "A" << std::endl;
+ return;
+ }
+ catch (std::exception &e)
+ {}
+ try
+ {
+ (void)dynamic_cast<B&>(p);
+ std::cout << "B" << std::endl;
+ return;
+ }
+ catch (std::exception &e)
+ {}
+ try
+ {
+ (void)dynamic_cast<C&>(p);
+ std::cout << "C" << std::endl;
+ return;
+ }
+ catch (std::exception &e)
+ {}
+
+ std::cout << "Couldn't identify from reference" << std::endl;
+}
+
int main()
{
- srand(time(NULL));
+ int seed;
+ std::ifstream devRandom("/dev/random");
+ if (devRandom.is_open())
+ {
+ devRandom.read((char*)&seed, sizeof(int));
+ devRandom.close();
+ }
+ else
+ {
+ seed = time(NULL);
+ }
+ srand(seed);
+
+ {
+ std::cout << "=============== IDENTIFY FROM POINTER ===============" << std::endl;
+ Base *a = new A();
+ Base *b = new B();
+ Base *c = new C();
+ identify_from_pointer(a);
+ identify_from_pointer(b);
+ identify_from_pointer(c);
+ delete a;
+ delete b;
+ delete c;
+ }
+
+ {
+ std::cout << "=============== IDENTIFY FROM REF ===============" << std::endl;
+ A a;
+ B b;
+ C c;
+ identify_from_reference(a);
+ identify_from_reference(b);
+ identify_from_reference(c);
+ }
+
+ std::cout << "=============== GENERATE ===============" << std::endl;
+ for (int i = 0; i < 10; i++)
+ {
+ Base *b = generate();
+ std::cout << "---" << std::endl;
+ identify_from_pointer(b);
+ identify_from_reference(*b);
+ delete b;
+ }
return 0;
}