当前位置 : 主页 > 编程语言 > c++ >

C++中继承 声明基类析构函数为虚函数,继承关系的内存分布

来源:互联网 收集:自由互联 发布时间:2021-06-23
1,基类析构函数不为虚函数 #include " pch.h " #include iostream class CBase{ public : CBase() { m_one = 0 ; printf( " this is CBase construct\n " ); } ~ CBase() { printf( " this is ~CBase deconstruct\n " ); } void setNumOne( int

 

1,基类析构函数不为虚函数

#include "pch.h"
#include <iostream>

class CBase
{
public:
    CBase() {
        m_one = 0;
        printf("this is CBase construct\n");
    }
    ~CBase() {
        printf("this is ~CBase deconstruct\n");
    }

    void setNumOne(int n)
    {
        m_one = n;
    }
    int getNumOne()
    {
        return m_one;
    }
private:
    int m_one;
};

class CDrived:public CBase
{
public:
    CDrived() {
        m_two = 0;
        printf("this is CDrived construct\n");
    }
    ~CDrived() {
        printf("this is ~CDrived deconstruct\n");
    }

    void setNumTwo(int n)
    {
        m_two = n;
    }
    int getNumTwo()
    {
        return m_two;
    }
private:
    int m_two;
};

int main()
{
    CBase *p = new CDrived;
    delete p;
    std::cout << "Hello World!\n"; 
}

输出:

this is CBase construct
this is CDrived construct
this is ~CBase deconstruct
Hello World!

可以发现继承类析构函数没有调用,若继承类中有一些资源需要释放,则不能释放,故需要将基类析构函数声明为虚函数。

 

#include "pch.h"
#include <iostream>

class CBase
{
public:
    CBase() {
        m_one = 0;
        printf("this is CBase construct\n");
    }
    virtual ~CBase() {
        printf("this is ~CBase deconstruct\n");
    }

    void setNumOne(int n)
    {
        m_one = n;
    }
    int getNumOne()
    {
        return m_one;
    }
private:
    int m_one;
};

class CDrived:public CBase
{
public:
    CDrived() {
        m_two = 0;
        printf("this is CDrived construct\n");
    }
    ~CDrived() {
        printf("this is ~CDrived deconstruct\n");
    }

    void setNumTwo(int n)
    {
        m_two = n;
    }
    int getNumTwo()
    {
        return m_two;
    }
private:
    int m_two;
};

int main()
{
    CBase *p = new CDrived;
    delete p;
    std::cout << "Hello World!\n"; 
}

输出:

this is CBase construct
this is CDrived construct
this is ~CDrived deconstruct
this is ~CBase deconstruct
Hello World!

 

2,

#include "pch.h"
#include <iostream>

class CBase
{
public:
    CBase() {
        m_one = 0;
        printf("this is CBase construct\n");
    }
    virtual ~CBase() {
        printf("this is ~CBase deconstruct\n");
    }

    virtual void setNumOne(int n)
    {
        m_one = n;
    }
    virtual int getNumOne()
    {
        return m_one;
    }
private:
    int m_one;
};

class CDrived:public CBase
{
public:
    CDrived() {
        m_two = 0;
        printf("this is CDrived construct\n");
    }
    ~CDrived() {
        printf("this is ~CDrived deconstruct\n");
    }

    void setNumTwo(int n)
    {
        m_two = n;
    }
    int getNumTwo()
    {
        return m_two;
    }
private:
    int m_two;
};

int main()
{
    CDrived *p = new CDrived;
    printf("sizeof(CDrived) = %d\n", sizeof(CDrived)); // 12
    delete p;
    std::cout << "Hello World!\n"; 
}

 

网友评论