mardi 5 juillet 2016

C++ i can`t understand why it call "father class static function" and it call just father destructor

I have next code:

#include <iostream>

using namespace std;

class A{
public:
    A() { cout << "A() ";}
    virtual void f()=0;
    virtual void g() { cout << "ag() ";}
    static void h() { cout << "ah() ";}
    void i() { cout << "ai() ";}
    ~A() { cout << "~A() "; }
};

class B: public A{
    void f() { cout << "bf() ";}
public:
    void g() { cout << "bg() ";}
};

class C: public B{
public:
    static void h() { cout << "ch() "; }
    void g() { cout << "cg() ";}
    ~C() { cout << "~C() ";}
};

void f1(){
    B b;
    C c;
    b.h();
    c.h();  
    A* arr[2] = { new B ,new C};
    arr[0]->h();
    arr[1]->h();
    delete arr[0];
    delete arr[1];
}


int main(){
    f1();
    return 0;
}

I can't understand why it prints A() A() ah() ch() A() A() ah() ah() ~A() ~A() ~C() ~A() ~A() and it doesn't print A() A() ah() ch() A() A() ah() ch() ~A() ~A() ~C() ~A() ~C(). Can anyone help me?

Aucun commentaire:

Enregistrer un commentaire