多态,多继承,虚函数,虚析构,vptr指针,初始化列表

 

源码


using namespace std;

class Parent1
{
public:
    Parent1() {
        cout << "Parent1()..." << endl;
        func();
    }
    virtual void func() {
        cout << "Parent1->func()..." << endl;
    }
    virtual ~Parent1() {
        cout << "~Parent1()..." << endl;
    }
private:
    int a;
};

class Parent2
{
public:
    Parent2() {
        cout << "Parent2()..." << endl;
        func();
    }
    virtual void func() {
        cout << "Parent2->func()..." << endl;
    }
    virtual ~Parent2() {
        cout << "~Parent2()..." << endl;
    }
private:
    int b;
};

class Child :public Parent2, public Parent1
{
public:
    Child():Parent2(), Parent1(){
        cout << "Child()..." << endl;
        func();
    }
    virtual void func()
    {
        cout << "Child-func() ..." << endl;
    }
    ~Child() {
        cout << "~Child()..." << endl;
    }
private:
    int c;
};


void test()
{
    Parent1 * p = new Child;

    p->func(); //没有多态

    delete p; //正常delete 没有问题

    Parent2 *p2 = new Child;
    p2->func();

    delete p2; //delete p2 ;; p2.~,  free(p2)    delete Child,  Child.~, free(child)
}

int main(void)
{
    test();

    return 0;
 

输出结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值