avr39ripe

cppTypeCastCPPWay

Sep 21st, 2021
603
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class Parent
  4. {
  5.     int id;
  6. public:
  7.     Parent() = default;
  8.     Parent(int idP) : id{ idP } { std::cout << "Parent ctor\n"; }
  9.     //virtual const std::string& getName(bool simple = true) const {};
  10.     virtual ~Parent() = default;
  11. };
  12.  
  13. class Child : public Parent
  14. {
  15.     char symb;
  16.     std::string name;
  17. public:
  18.     Child(int idP, char symbP, std::string nameP) : Parent{ idP }, symb{ symbP }, name{nameP} { std::cout << "Child ctor\n"; };
  19.     const std::string& getName(bool simple = true) const
  20.     {
  21.         if(simple) return name;
  22.         return name + " class symbol -> " + symb;
  23.     }
  24.     std::string& getName(bool simple = true)
  25.     {
  26.         if (simple) return name;
  27.         return name += " class symbol -> " + symb;
  28.     }
  29. };
  30.  
  31. class Other
  32. {
  33.  
  34. };
  35.  
  36. void processParents(Parent& p)
  37. {
  38.     // try to proces different Parent descendants polymorphically
  39.  
  40. }
  41.  
  42. int main()
  43. {
  44.     // static_cast
  45.     // dynamic_cast
  46.     // reinterpret_cast
  47.     // const_cast
  48.  
  49.     //int numI{ 42 };
  50.     //float numF{ 36.6 };
  51.     //int* ptrI{};
  52.     //float* ptrF{};
  53.     //void* ptrV{ &ptrF };
  54.  
  55.     //ptrI = reinterpret_cast<int*>(ptrF);
  56.     //ptrI = (int*)ptrF;
  57.     //std::cout << (static_cast<float>(numI) / 9) << '\n';
  58.  
  59.     Parent* ptrP{};
  60.     //ptrP = static_cast<Other*>(new Child{ 42,'z' });
  61.     //ptrP = static_cast<Parent*>(new Child{ 42,'z',"Straustrup" });
  62.  
  63.     ptrP = new Child{ 42,'z',"Straustrup" };
  64.     //ptrP = new Parent(13);
  65.     //Parent pappi{ };
  66.     //pappi = *ptrP;
  67.  
  68.     //std::cout << typeid(*ptrP).name() << " is Parent ? " << std::boolalpha << (typeid(*ptrP) == typeid(Parent)) << '\n';
  69.  
  70.     //std::cout << dynamic_cast<Child*>(ptrP)->getName() << '\n';;
  71.  
  72.     Parent* effectivePtr{ dynamic_cast<Child*>(ptrP) };
  73.     //Parent* effectivePtr{ (Child*)(ptrP) };
  74.  
  75.     if (effectivePtr)
  76.     {
  77.         std::cout << "we've got child! ";
  78.         std::cout << dynamic_cast<Child*>(effectivePtr)->getName() << '\n';
  79.  
  80.     }
  81.     else
  82.     {
  83.         std::cout << "we've got parent! ";
  84.     }
  85.  
  86.  
  87.     //if (typeid(*ptrP) == typeid(Child))
  88.     //{
  89.     //  std::cout << "We've got Child! ";
  90.     //  std::cout << (((Child*)ptrP)->getName()) << '\n';
  91.  
  92.     //}
  93.     //else
  94.     //{
  95.     //  std::cout << "We've got Parent! ";
  96.     //}
  97.  
  98.     return 0;
  99. }
  100. // RTTI - Runtime type identification
  101. // A -> B -> C
  102.  
RAW Paste Data