Advertisement
avr39ripe

cppCppStyleCastAdv

Sep 23rd, 2021
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  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.         return const_cast<std::string&>((const_cast<const Child*>(this))->getName());
  30.     }
  31. };
  32.  
  33. class Other
  34. {
  35.  
  36. };
  37.  
  38. void processParents(Parent& p)
  39. {
  40.     // try to proces different Parent descendants polymorphically
  41.  
  42. }
  43.  
  44.  
  45. template <typename T>
  46. void typeInfo(const T& obj)
  47. {
  48.     std::cout << "Real type of object is: " << typeid(obj).name() << '\n';
  49. }
  50.  
  51.  
  52. int strLen(const char* str)
  53. {
  54.     int len{};
  55.     for (; *str++; ++len);
  56.     return len;
  57. }
  58.  
  59. void childInfo(const Child& ch)
  60. {
  61.     std::cout << "This is Child object with name -> " << ch.getName() << '\n';
  62. }
  63.  
  64.  
  65.  
  66. int main()
  67. {
  68.     // static_cast
  69.     // dynamic_cast
  70.     // reinterpret_cast
  71.     // const_cast
  72.  
  73.     //int numI{ 42 };
  74.     //float numF{ 36.6 };
  75.     //int* ptrI{};
  76.     //float* ptrF{};
  77.     //void* ptrV{ &ptrF };
  78.  
  79.     //ptrI = reinterpret_cast<int*>(ptrF);
  80.     //ptrI = (int*)ptrF;
  81.     //std::cout << (static_cast<float>(numI) / 9) << '\n';
  82.  
  83.     /*Parent* ptrP{};*/
  84.     ////ptrP = static_cast<Other*>(new Child{ 42,'z' });
  85.     ////ptrP = static_cast<Parent*>(new Child{ 42,'z',"Straustrup" });
  86.  
  87.     //ptrP = new Child{ 42,'z',"Straustrup" };
  88.     //ptrP = new Parent(13);
  89.     //Parent pappi{ };
  90.     //pappi = *ptrP;
  91.  
  92.     //std::cout << typeid(*ptrP).name() << " is Parent ? " << std::boolalpha << (typeid(*ptrP) == typeid(Parent)) << '\n';
  93.  
  94.     //std::cout << dynamic_cast<Child*>(ptrP)->getName() << '\n';;
  95.  
  96.     //Child* effectivePtr{ static_cast<Child*>(ptrP) };
  97.     //Parent* effectivePtr{ (Child*)(ptrP) };
  98.  
  99.     //if (effectivePtr)
  100.     //{
  101.     //  std::cout << "we've got child! ";
  102.     //  //std::cout << dynamic_cast<Child*>(effectivePtr)->getName() << '\n';
  103.     //  std::cout << effectivePtr->getName() << '\n';
  104.  
  105.     //}
  106.     //else
  107.     //{
  108.     //  std::cout << "we've got parent! ";
  109.     //}
  110.  
  111.  
  112.  
  113.  
  114.     //if (typeid(*ptrP) == typeid(Child))
  115.     //{
  116.     //  std::cout << "We've got Child! ";
  117.     //  std::cout << (((Child*)ptrP)->getName()) << '\n';
  118.  
  119.     //}
  120.     //else
  121.     //{
  122.     //  std::cout << "We've got Parent! ";
  123.     //}
  124.  
  125.     //Parent p;
  126.     //Parent& refParent{ p };
  127.     //Child child{ 11,'g',"Bill" };
  128.     //Parent& refParent{ child };
  129.  
  130.     //try
  131.     //{
  132.     //  Child& refChild{ static_cast<Child&>(refParent) };
  133.  
  134.  
  135.     //  std::cout << refChild.getName() << '\n';
  136.     //}
  137.     //catch (std::exception ex)
  138.     //{
  139.     //  std::cout << ex.what() << '\n';
  140.     //}
  141.     //catch (...)
  142.     //{
  143.     //  std::cout << "OOps! :(" << '\n';
  144.     //}
  145.     //typeInfo(23);
  146.     //typeInfo('z');
  147.     //typeInfo(Parent{});
  148.     //typeInfo(Child{1,'z',"Gates"});
  149.    
  150.     //const int answer{ 42 };
  151.  
  152.     //int& refAnswer{ const_cast<int&>(answer) };
  153.     ////int& refAnswer{ answer };
  154.  
  155.     //refAnswer = 13;
  156.  
  157.     //std::cout << "refAnswer = " << refAnswer << '\n';
  158.     //std::cout << "answer = " << answer << '\n';
  159.  
  160.     /*const char* str{ new char[20]{"Hello, world!"} };
  161.  
  162.     std::cout << "Length of str is: " << strLen(const_cast<char*>(str)) << '\n';
  163.  
  164.     const Child ch{ 1,'z',"Baaddy" };
  165.  
  166.     childInfo(const_cast<Child&>(ch));
  167.     childInfo(ch);
  168.  
  169.     delete[] str;*/
  170.  
  171.     //int answer{ 42 };
  172.     //const int* ptr{ &answer };
  173.  
  174.     //*(const_cast<int*>(ptr)) = 13;
  175.  
  176.     Child ch{ 1,'z',"Baaddy" };
  177.  
  178.     std::cout << ch.getName() << '\n';
  179.  
  180.     return 0;
  181. }
  182. // RTTI - Runtime type identification
  183. // A -> B -> C
  184.     // B-D -E  
  185.  
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement