fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class A {
  7. public:
  8. A(std::string s, int x) : m_s(s), m_x(x) {}
  9.  
  10. void print() { cout << "A::print(): str = " << m_s << ", x = " << m_x << endl; }
  11.  
  12. private:
  13. std::string m_s;
  14. int m_x;
  15. };
  16.  
  17. class B {
  18. public:
  19. B(int x) : m_x(x) {}
  20. void print() { m_x++; cout << "B::print(): x = " << m_x << endl; }
  21.  
  22. private:
  23. int m_x;
  24.  
  25. };
  26.  
  27. int main(int argc, char **argv) {
  28.  
  29. A *a = new A("abc", 1);
  30. a->print();
  31. B *b = (B*)a;
  32. b->print();
  33.  
  34. a->print();
  35.  
  36. return 0;
  37. }
Runtime error #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
A::print(): str = abc, x = 1
B::print(): x = 158718493
A::print(): str = bc�u	�