Base.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   Base.cpp                                           :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/12/18 03:05:05 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/12/21 17:16:18 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "Base.hpp"
 14  #include "A.hpp"
 15  #include "B.hpp"
 16  #include "C.hpp"
 17  
 18  /* ************************************************************************** */
 19  /*                          Constructor & Destructor                          */
 20  /* ************************************************************************** */
 21  
 22  Base::~Base()
 23  {
 24  	// nothing to do
 25  }
 26  
 27  /* ************************************************************************** */
 28  /*                             Exported Functions                             */
 29  /* ************************************************************************** */
 30  
 31  Base*	generate(void)
 32  {
 33  	srand(time(0));
 34  
 35  	switch (rand() % 3)
 36  	{
 37  		case 0:
 38  			return new A();
 39  		case 1:
 40  			return new B();
 41  		case 2:
 42  			return new C();
 43  		default:
 44  			; // unreachable
 45  	}
 46  	return 0;
 47  }
 48  
 49  void	identify(Base* p)
 50  {
 51  	if (dynamic_cast<A*>(p))
 52  	{
 53  		std::cout << "A*" << std::endl;
 54  	}
 55  	else if (dynamic_cast<B*>(p))
 56  	{
 57  		std::cout << "B*" << std::endl;
 58  	}
 59  	else if (dynamic_cast<C*>(p))
 60  	{
 61  		std::cout << "C*" << std::endl;
 62  	}
 63  	else
 64  	{
 65  		throw std::runtime_error("Bad Pointer");
 66  	}
 67  }
 68  
 69  void	identify(Base& p)
 70  {
 71  	bool	flag = false;
 72  
 73  	try
 74  	{
 75  		A&	a __attribute__((unused)) = dynamic_cast<A&>(p);
 76  		std::cout << "A&" << std::endl;
 77  		flag = true;
 78  	}
 79  	catch (...)
 80  	{
 81  		// nothing to do
 82  	}
 83  	try
 84  	{
 85  		B&	b __attribute__((unused)) = dynamic_cast<B&>(p);
 86  		std::cout << "B&" << std::endl;
 87  		flag = true;
 88  	}
 89  	catch (...)
 90  	{
 91  		// nothing to do
 92  	}
 93  	try
 94  	{
 95  		C&	c __attribute__((unused)) = dynamic_cast<C&>(p);
 96  		std::cout << "C&" << std::endl;
 97  		flag = true;
 98  	}
 99  	catch (...)
100  	{
101  		// nothing to do
102  	}
103  
104  	if (flag == false)
105  	{
106  		throw std::runtime_error("Bad Reference");
107  	}
108  	else
109  	{
110  		// identify success
111  	}
112  }