This commit is contained in:
Camille Chauvet 2023-09-20 16:04:18 +00:00
parent 55329e73c2
commit 6dfaa9b245
10 changed files with 91 additions and 15 deletions

7
.gitignore vendored
View File

@ -1,2 +1,5 @@
ex0*/obj/*.o
ex00/convert
*
!/**/
!*.*
!/**/Makefile
*.o

2
ex00/.gitignore vendored
View File

@ -1,2 +0,0 @@
obj/*.o
convert

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,10 +6,14 @@
#include "B.hpp"
#include "C.hpp"
Base* genere(void)
Base* generate(void)
{
static bool generated = false;
if (generated == false)
{
std::srand(time(NULL));
generated = !generated;
}
switch (rand() % 3) {
case 0:

5
ex02/src/generate.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "Base.hpp"
Base* generate(void);

View File

@ -3,15 +3,17 @@
#include "B.hpp"
#include "C.hpp"
#include <exception>
#include <iostream>
#include <stddef.h>
void indentify(Base* p)
void identify(Base* p)
{
if (dynamic_cast<A*>(p) == nullptr)
if (dynamic_cast<A*>(p) != NULL)
std::cout << "A" << std::endl;
else if (dynamic_cast<B*>(p) == nullptr)
else if (dynamic_cast<B*>(p) != NULL)
std::cout << "B" << std::endl;
else if (dynamic_cast<C*>(p) == nullptr)
else if (dynamic_cast<C*>(p) != NULL)
std::cout << "C" << std::endl;
}
@ -19,20 +21,23 @@ void identify(const Base &r)
{
try
{
(void)dynamic_cast<A>(r);
(void)dynamic_cast<const A&>(r);
std::cout << "A" << std::endl;
return;
}
catch (std::exception& e) {}
try
{
(void)dynamic_cast<B>(r)
(void)dynamic_cast<const B&>(r);
std::cout << "B" << std::endl;
return;
}
catch (std::exception& e) {}
try
{
(void)dynamic_cast<C>(r)
(void)dynamic_cast<const C&>(r);
std::cout << "C" << std::endl;
return;
}
catch (std::exception& e) {}
}

View File

@ -3,4 +3,4 @@
#include "Base.hpp"
void identify(Base* p);
void identify(Base& p);
void identify(const Base& p);

61
ex02/src/main.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "A.hpp"
#include "B.hpp"
#include "Base.hpp"
#include "C.hpp"
#include "identify.hpp"
#include "generate.hpp"
#include <iostream>
int main()
{
std::cout << "identify PTR A: ";
{
A tmp;
identify(&tmp);
}
std::cout << std::endl;
std::cout << "identify PTR B: ";
{
B tmp;
identify(&tmp);
}
std::cout << std::endl;
std::cout << "identify PTR C: ";
{
C tmp;
identify(&tmp);
}
std::cout << std::endl;
std::cout << "identify REF A: ";
{
A tmp;
identify(tmp);
}
std::cout << std::endl;
std::cout << "identify REF B: ";
{
B tmp;
identify(tmp);
}
std::cout << std::endl;
std::cout << "identify REF C: ";
{
C tmp;
identify(tmp);
}
std::cout << std::endl;
for (int i = 0; i != 10; i++)
{
Base* tmp = generate();
identify(tmp);
delete(tmp);
std::cout << std::endl;
}
}