c++虚析构函数的使用及其注意点
2024-09-04 17:14:48
// ConsoleApplication33.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
class A
{
public: A(A* a)
{
cout << "1" << endl;
}
A(const A&a)
{ cout << "A copy" << this << endl;
}
A()
{
p = new char[20];
strcpy_s(p, 2, "a");
cout<<"A():"<<this << endl;
}
virtual ~A()
{
if(p!=NULL)
delete[] p;
cout<<" ~A():"<<this << endl;
}
int k = 4;
public:
char *p; };
class B :public A
{
public:
B()
{
p = new char[20];
strcpy_s(p, 3, "aa");
cout<<"B():" << this <<endl;
}
~B()
{
delete[] p;
cout << "~B():" << this << endl;
}
public:
char *p; }; class C :public B
{
public:
C()
{
p = new char[20];
strcpy_s(p, 4, "aaa");
cout << "C():" << this << endl;
}
~C()
{
delete[] p;
cout << "~C():" << this << endl;
}
public:
char *p;
int m = 3; }; void howtodelete(A* base)
{
delete base;
} int main()
{ A* a = NULL;
//注意 使用指针这里一定要new 然后才能用父类指针去接收 否则会出错 new delete要配对
C* c1=new C;
a = &(*c1);
howtodelete(a); system("pause");
}
最新文章
- AngularJS依赖注入
- Angular内置指令(一)
- iOS开发之手势识别
- ios 图片截取功能 图片拼接功能
- GridLookUpEdit 简单应用
- 转贴:C++中指针和引用的区别
- BZOJ2739 最远点(分治 + 决策单调性)
- 武道释义 &;#183; 零散
- ASP.NET Core MVC之Serilog日志处理,你了解多少?
- C# 封装miniblink 使用HTML/CSS/JS来构建.Net 应用程序界面和简易浏览器
- Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表
- windows 共享文件
- springboot-mybatis多数据源以及踩坑之旅
- thinkphp 5.1 同时选中多个文件上传
- Luogu4528 CTSC2008 图腾 树状数组、容斥
- Is it possible to display icons in a PopupMenu?
- tomcat nginx默许的post大小限制
- tomcat7部署多个web应用不同编码,端口
- 浅谈iOS中MVVM的架构设计
- 13-[函数进阶]-列表生成式,生成器&;迭代器