finish
This commit is contained in:
@ -6,10 +6,14 @@
|
||||
#include "B.hpp"
|
||||
#include "C.hpp"
|
||||
|
||||
Base* genere(void)
|
||||
Base* generate(void)
|
||||
{
|
||||
|
||||
std::srand(time(NULL));
|
||||
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
5
ex02/src/generate.hpp
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "Base.hpp"
|
||||
|
||||
Base* generate(void);
|
@ -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) {}
|
||||
}
|
||||
|
@ -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
61
ex02/src/main.cpp
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user