Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A{
- public:
- virtual void f(){ printf("A.f "); }
- ~A(){ f(); }
- };
- class B {
- public:
- A a;
- void f(){ printf("B.f "); }
- B(int i=0){
- if(!i) throw 0;
- printf("%d ", i);
- }
- ~B(){ f(); }
- };
- int zad4(){
- B b(12); // (1)
- try {
- A a; // (2)
- B* b = new B(); // (3)
- delete b; // (4)
- }catch (int e){
- printf("Exc "); // (5)
- }
- return 0; // (6)
- }
- /* 12 A.f A.f Exc B.f A.f
- (1) Konstruktor B:
- - konstruktor A (obiekt w B) - nic nie wypisuje
- - konstruktor B - brak błędu - wypisuje 12 12
- (2) Konstuktor A:
- - nic nie wypisuje
- (3) Kostruktor B:
- - konstruktor A (obiekt w B) - nic nie wypisuje
- - konstruktor B - wyrzuca błąd
- (4) Nie wykonuje się
- (*) wyrzucony błąd powoduje uruchomienie destruktorów:
- - destruktor z (2) - wypisuje A.f A.f
- - destruktor z (3) - wypisuje A.f A.f
- (5) Wypisuje Exc Exc
- (6) Destruktor B z (1) odwrotna kolejność:
- - destruktor B - wypisuje B.f B.f
- - destruktor A (obiekt w B) - wypisuje A.f A.f
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement