zoukankan      html  css  js  c++  java
  • c++-虚析构函数

    虚析构函数

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    class A
    {
    public:
    	A() {
    		cout << "A()..." << endl;
    		this->p = new char[64];
    		memset(this->p, 0, 64);
    		strcpy(this->p, "A String..");
    	}
    
    	virtual void print()
    	{
    		cout << "A: " << this->p << endl;
    	}
    
    	virtual ~A() {
    		cout << "~A()..." << endl;
    		if (this->p != NULL) {
    			delete[]this->p;
    			this->p = NULL;
    		}
    	}
    private:
    	char *p;
    };
    
    class B :public A
    {
    public:
    	B() //此刻会触发A()
    	{
    		cout << "B()..." << endl;
    		this->p = new char[64];
    		memset(this->p, 0, 64);
    		strcpy(this->p, "B String..");
    	}
    
    	virtual void print()
    	{
    		cout << "B: " << this->p << endl;
    	}
    
    	virtual ~B() {
    		cout << "~B()..." << endl;
    		if (this->p != NULL) {
    			delete[] this->p;
    			this->p = NULL;
    		}
    	}
    private:
    	char *p;
    };
    
    
    void func(A *ap)
    {
    	ap->print();//在此发生多态
    
    }
    
    void deleteFunc(A *ap)
    {
    	delete ap; //此刻ap->~B() //~B() ---> ~A()
    }
    
    void test()
    {
    	//A *ap = new A;
    	//func(ap);
    	B *bp = new B;
    	func(bp);
    
    	deleteFunc(bp);
    }
    
    int main(void)
    {
    	test();
    	
    
    	B bObj;
    
    	//bObj.~B();
    	return 0;
    }
    
  • 相关阅读:
    兄弟连学python(1)——MySQL
    运算和运算符相关知识
    关于python中的快捷键
    关于爬虫
    Hello Python
    [ARC101C] Ribbons on Tree
    CF568E Longest Increasing Subsequence
    2021省选游记
    [NEERC2015]Distance on Triangulation
    dp的一些优化
  • 原文地址:https://www.cnblogs.com/ygjzs/p/12079025.html
Copyright © 2011-2022 走看看