Private Destructor
Predict the output of following programs.
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test()
8 {
9 }
10 };
11 int main()
12 {
13 }
The above program compiles and runs fine. It is not compiler error to create private destructors.
What do you say about below program.
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test()
8 {
9 }
10 };
11 int main()
12 {
13 Test t;
14 return 0;
15 }
The above program fails in compilation.
The compiler notices that the local variable ‘t’ cannot be destructed because the destructor is private.
What about the below program?
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test() {}
8 };
9 int main()
10 {
11 Test *t;
12 }
The above program works fine. There is no object being constructed, the program just creates a pointer of type “Test *”, so nothing is destructed.
What about the below program?
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test() {}
8 };
9 int main()
10 {
11 Test *t = new Test;
12 }
The above program also works fine. When something is created using dynamic memory allocation, it is programmer’s responsibility to delete it. So compiler doesn’t bother.【没有delete t,所以不会调用private 析构函数,因此不会出错哦】
The below program fails in compilation. When we call delete, desturctor is called.
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test() {}
8 };
9 int main()
10 {
11 Test *t = new Test;
12 delete t;
13 }
We noticed in the above programs, when a class has private destructur, only dynamic objects of that class can be created.
Following is a way to create classes with private destructors and have a function as friend of the class. The function can only delete the objects.
1 #include <iostream>
2
3 // A class with private destuctor
4 class Test
5 {
6 private:
7 ~Test() {}
8 friend void destructTest(Test* );
9 };
10
11 // Only this function can destruct objects of Test
12 void destructTest(Test* ptr)
13 {
14 delete ptr;
15 }
16
17 int main()
18 {
19 // create an object
20 Test *ptr = new Test;
21
22 // destruct the object
23 destructTest (ptr);
24
25 return 0;
26 }
What is the use of private destructor?
Whenever we want to control destruction of objects of a class, we make the destructor private. For dynamically created objects, it may happen that you pass a pointer to the object to a function and the function deletes the object. If the object is referred after the function call, the reference will become dangling. See this for more details
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 10:48:14
Private Destructor的更多相关文章
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- C++模拟C#事件委托机制(二)
原文 来自于http://www.cnblogs.com/netssfy/archive/2010/02/02/1662056.html 为了解决非法地址访问的冲突,首先需要知道发生该错误的原因是什么 ...
- 私有析构函数 Android 代码分析
有人说声明 Private Destructor, 这对象只能在 stack 上创建,不能在Heap上创建, 其实错了, 这样的程序编译都过不了. 那为何会有 Private Destructor, ...
- “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册
原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂
C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...
- C++ Knowledge series Inheritance & RTTI & Exception Handling
Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...
- C++中public,protected,private派生类继承问题和访问权限问题
C++中public,protected,private派生类继承问题和访问权限问题 当一个子类从父类继承时,父类的所有成员成为子类的成员,此时对父类成员的访问状态由继承时使用的继承限定符决定. 1. ...
- 构造函数constructor 与析构函数destructor(五)
我们知道当调用默认拷贝构造函数时,一个对象对另一个对象初始化时,这时的赋值时逐成员赋值.这就是浅拷贝,当成员变量有指针时,浅拷贝就会在析构函数那里出现问题.例如下面的例子: //test.h #ifn ...
随机推荐
- SpringCloud 2020.0.4 系列之 Sleuth + Zipkin
1. 概述 老话说的好:安全不能带来财富,但盲目的冒险也是不可取的,大胆筹划,小心实施才是上策. 言归正传,微服务的特点就是服务多,服务间的互相调用也很复杂,就像一张关系网,因此为了更好的定位故障和优 ...
- MySQL高级篇 | MySQL逻辑架构
思维导图 架构逻辑视图 每个虚线框为一层,总共三层. 第一层:连接层,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等. 第二 ...
- Java基础复习之数组
Java基础复习之:数组 简介 数组(Array):多个相同数据类型按照一定顺序排列的集合,并使用一个名字命名,通过编号的方式对这些数据进行统一管理 一维数组 一维数组的声明与初始化 int[] id ...
- Bootstrap-2栅格系统
栅格系统(使用最新版本bootstrap) Grid options(网格配置) Responsive classes(响应式class) Gutters(间距) Alignment(对齐方式) Re ...
- fabric运行记录
创建第一个fabric网络 Generate Network Artifacts cd first-network 先关闭服务 ./byfn.sh -m down 然后创建 ./byfn.sh -m ...
- 自由导入你的增量数据-根据条件将sqlserver表批量生成INSERT语句的存储过程实施笔记
文章标题: 自由导入你的增量数据-根据条件将sqlserver表批量生成INSERT语句的存储过程增强版 关键字 : mssql-scripter,SQL Server 文章分类: 技术分享 创建时间 ...
- 让textarea根据文本的长度自动调整它的高度
... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR ...
- GO的安装以及GoLand破解
GO的安装以及GoLand破解 GO的安装 GO语言中文网:GO语言中文网 go,GoLand,破解文件:JetBrains GoLand 2019.2.3 x64 提取码:ABCD(汉化文件也在其中 ...
- C# 复制目录及其下文件
public static void CopyDirectory(string sourceDirPath, string saveDirPath) { try { if (!Directory.Ex ...
- spring-整合es
spring-整合es 导入pom <?xml version="1.0" encoding="UTF-8"?> <project xmln ...