CPP   83

cast

Guest on 19th May 2022 02:00:07 AM

  1. #include <iostream>
  2.  
  3. struct A {
  4.     int foo;
  5.  
  6.     A() : foo(3) {}
  7. };
  8.  
  9. struct B {
  10.     int bar;
  11.  
  12.     B() : bar(4) {}
  13. };
  14.  
  15. struct C : public A, public B {
  16. };
  17.  
  18. int main() {
  19.     const C* c = new C();
  20.  
  21.     // Cast away const, and also convert to base class B.
  22.     B* b = (B*)c;
  23.     std::cout << "bar = " << b->bar << std::endl;
  24.  
  25.     delete c;
  26.     return 0;
  27. }

Raw Paste


Login or Register to edit or fork this paste. It's free.