SHARE
TWEET

Untitled

a guest Jun 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4.  
  5. using namespace std; //fuj
  6.  
  7. //#LK: zakomentuj makro, zeby wylaczyc wirtualny destruktor
  8. #define VIRTUAL_DESTRUCTOR
  9.  
  10. class BaseClass
  11. {
  12. public:
  13.     BaseClass(const std::string& name)
  14.         : m_Name(name)
  15.     {
  16.         cout << "BaseClass::" << m_Name << " constructor called" << endl;
  17.     }
  18.  
  19. #ifdef VIRTUAL_DESTRUCTOR
  20.     virtual ~BaseClass()
  21.     {
  22.         cout << "BaseClass::" << m_Name << " virtual destructor called" << endl << endl;
  23.     }
  24. #else
  25.     ~BaseClass()
  26.     {
  27.         cout << "BaseClass::" << m_Name << " destructor called" << endl << endl;
  28.     }
  29. #endif //VIRTUAL_DESTRUCTOR
  30.  
  31.     std::string m_Name;
  32. };
  33.  
  34. class DerivedClass : public BaseClass
  35. {
  36. public:
  37.     DerivedClass(const std::string& name)
  38.         : BaseClass(name)
  39.     {
  40.         cout << "DerivedClass::" << m_Name << " constructor called" << endl;
  41.     }
  42.     ~DerivedClass()
  43.     {
  44.         cout << "DerivedClass::" << m_Name << " destructor called" << endl;
  45.     }
  46.  
  47.     int dupa = 5;
  48. };
  49.  
  50. int main()
  51. {
  52.     BaseClass* ptr = new DerivedClass("dupa");
  53.     BaseClass obj = *ptr;
  54.  
  55.     DerivedClass* fail    = dynamic_cast<DerivedClass*>(&obj);
  56.     DerivedClass* success = dynamic_cast<DerivedClass*>(ptr);
  57.  
  58.     return 0;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top