zoukankan      html  css  js  c++  java
  • C++中继承 声明基类析构函数为虚函数作用,单继承和多继承关系的内存分布

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

    #include "pch.h"
    #include <iostream>
    
    class CBase
    {
    public:
        CBase() {
            m_one = 0;
            printf("this is CBase construct
    ");
        }
        ~CBase() {
            printf("this is ~CBase deconstruct
    ");
        }
    
        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
    ");
        }
        ~CDrived() {
            printf("this is ~CDrived deconstruct
    ");
        }
    
        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!
    "; 
    }

    输出:

    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
    ");
        }
        virtual ~CBase() {
            printf("this is ~CBase deconstruct
    ");
        }
    
        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
    ");
        }
        ~CDrived() {
            printf("this is ~CDrived deconstruct
    ");
        }
    
        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!
    "; 
    }

    输出:

    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
    ");
        }
        virtual ~CBase() {
            printf("this is ~CBase deconstruct
    ");
        }
    
        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
    ");
        }
        ~CDrived() {
            printf("this is ~CDrived deconstruct
    ");
        }
    
        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
    ", sizeof(CDrived)); // 12
        delete p;
        std::cout << "Hello World!
    "; 
    }

    3,多继承

    单继承只有一个虚表指针,而多继承往往有多个

    #include "pch.h"
    #include <iostream>
    
    class CFather
    {
    public:
        CFather() {
        
        }
        ~CFather() {
    
        }
    
        virtual void setTall(int tall)
        {
            m_tall = tall;
        }
    
    private:
        int m_tall;
    };
    
    class CMother
    {
    public:
        CMother() {
    
        }
        ~CMother() {
    
        }
    
        virtual void setWeight(int weight)
        {
            m_weight = weight;
        }
    
    private:
        int m_weight;
    };
    
    class CSon:public CFather,public CMother
    {
    public:
        CSon() {
    
        }
        ~CSon() {
    
        }
    
        virtual void setAge(int age) // 地址存放在第一个虚表指针后面
        {
            m_age = age;
        }
    
    private:
        int m_age;
    };
    
    int main()
    {
        CSon cSon;
        cSon.setAge(8);
        printf("sizeof(CSon) = %d
    ", sizeof(CSon)); // 20
        std::cout << "Hello World!
    "; 
    }

  • 相关阅读:
    Delphi Variant 通用类型[3] 流 Stream的相互转换
    Delphi System单元 Utf8ToAnsi、AnsiToUtf8、Utf8Decode、Utf8Encode、Utf8ToUnicode、UnicodeToUtf8 转换
    OCR (Optical Character Recognition,光学字符识别)
    使用Python写Windows Service服务程序
    双精度张量内核加快了高性能计算
    A100计算能力
    A100 Tensor核心可加速HPC
    A100 GPU硬件架构
    NVIDIA深度架构
    稀疏性如何为AI推理增加难度
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/11471079.html
Copyright © 2011-2022 走看看