Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. class A{
  2. public:
  3.     virtual void f(){ printf("A.f "); }
  4.     ~A(){ f(); }
  5. };
  6.  
  7. class B {
  8. public:
  9.     A a;
  10.     void f(){ printf("B.f "); }
  11.     B(int i=0){
  12.         if(!i) throw 0;
  13.         printf("%d ", i);
  14.     }
  15.     ~B(){ f(); }
  16. };
  17.  
  18. int zad4(){
  19.     B b(12);            //   (1)
  20.     try {
  21.         A a;            //   (2)
  22.         B* b = new B(); //   (3)
  23.         delete b;       //   (4)
  24.     }catch (int e){
  25.         printf("Exc "); //   (5)
  26.     }
  27.     return 0;           //   (6)
  28. }
  29.  
  30. /*    12  A.f  A.f   Exc  B.f  A.f
  31.  
  32.     (1) Konstruktor B:
  33.         - konstruktor A (obiekt w B) - nic nie wypisuje
  34.         - konstruktor B - brak błędu - wypisuje 12                     12
  35.     (2) Konstuktor A:
  36.         - nic nie wypisuje
  37.     (3) Kostruktor B:
  38.         - konstruktor A (obiekt w B) - nic nie wypisuje
  39.         - konstruktor B - wyrzuca błąd
  40.     (4) Nie wykonuje się
  41.     (*) wyrzucony błąd powoduje uruchomienie destruktorów:
  42.         - destruktor z (2) - wypisuje  A.f                             A.f
  43.         - destruktor z (3) - wypisuje A.f                              A.f
  44.     (5) Wypisuje Exc                                                   Exc
  45.     (6) Destruktor B z (1) odwrotna kolejność:
  46.         - destruktor B - wypisuje B.f                                  B.f
  47.         - destruktor A (obiekt w B) - wypisuje A.f                     A.f
  48.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement