C++ Constructor And Destructor
if you have problems with constructors and destructors, you can insert such print statements in constructors for your real classes to see that they work as intended. For larger programs, this exact kind of tracing becomes tedious, but similar techniques apply.
For example, you can determine whether you have a memory leak by seeing if the number of constructions minus the number of destructions equals zero. Forgetting to define copy constructors and copy assignments for classes that allocate memory or hold pointers to objects is a common — and easily avoidable — source of problems.
when does those happen? A good way to get a feel for that is to add print
statements to constructors, assignment operations, and destructors and then just
try.
For example:
struct X {
int val;
// simple test class
void out(const string& s, int nv)
{ cerr << this << "–>" << s << ": " << val << " (" << nv << ")\n"; }
X(){ out("X()",0); val=0; }
// default constructor
X(int v) { val=v; out( "X(int)",v); }
X(const X& x){ val=x.val; out("X(X&) ",x.val); }
// copy constructor
X& operator=(const X& a)
// copy assignment
{ out("X::operator=()",a.val); val=a.val; return *this; }
~X() { out("~X()",0); }
// destructor
};
X glob(2); // a global variable
X copy(X a) { return a; }
X copy2(X a) { X aa = a; return aa; }
X& ref_to(X& a) { return a; }
X* make(int i) { X a(i); return new X(a); }
struct XX { X a; X b; };
int main(){
X loc {4}; // local variable
X loc2 {loc}; // copy construction
loc = X{5}; // copy assignment
loc2 = copy(loc); // call by value and return
loc2 = copy2(loc);
X loc3 {6};
X& r = ref_to(loc); // call by reference and return
delete make(7);
delete make(8);
vector<X> v(4); // default values
XX loc4;
X* p = new X{9}; // an X on the free store
delete p;
X* pp = new X[5]; // an array of Xs on the free store
delete[ ] pp;
}
Try executing this.
C++ Constructor And Destructor的更多相关文章
- __attribute__中constructor和destructor
1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...
- Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization
w https://zh.wikipedia.org/wiki/RAII RAII要求,资源的有效期与持有资源的对象的生命期严格绑定,即由对象的构造函数完成资源的分配(获取),同时由析构函数完成资源的 ...
- __attribute__中constructor和destructor[总结]
1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...
- Constructor and destructor -- Initialization & Cleanup in C++
Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- C++ 类 复制构造函数 The Copy Constructor
一.复制构造函数的定义 复制构造函数是一种特殊的构造函数,具有一般构造函数的所有特性.复制构造函数创建一个新的对象,作为另一个对象的拷贝.复制构造函数只含有一个形参,而且其形参为本类对象的引用.复制构 ...
- 深度探索C++对象模型之第二章:构造函数语意学之Default constructor的构造操作
C++新手一般由两个常见的误解: 如果任何class没有定义默认构造函数(default constructor),编译器就会合成一个来. 编译器合成的的default constructor会显示的 ...
- TAQSkinScrollBar 类美化滚动条再讨论
再说:TAQSkinScrollBar 类美化滚动条,http://www.138soft.com/?p=156 里面有人提到不可以滚动 滚动的改善方法: unit AQSkinScrollBar; ...
- Delphi 关键字详解[整理于 "橙子" 的帖子]
absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var Str: ]; StrLen: Byte absolute Str; //这个声明指定了变量 ...
- TLogger一个D7可用的轻量级日志
今天调程序,要用到日志.XE7有Qlog,D7用什么 从网上找到了Logger,下载的原文是不支持D7的,不过也只是很少的地方不同,自己修改了下就可以用了 感谢原作者和红鱼的分享 unit Logge ...
随机推荐
- PageOffice既保存Word文件中指定区域的数据又保存整篇文件
一.首先在word文件中给需要在后台获取数据的区域设置以PO_开头的书签. 二.通过pageoffice在线打开文件并编辑保存.有两种打开文件的模式 1.普通编辑模式(docNormalEdit) 普 ...
- java学习之旅(day.20)
注解和反射 注释comment:给人看 注解annotation:不仅可以给人看,还能给程序看,甚至能被其他程序读取 注解入门 什么是注解 注解的作用: 不是程序本身,可以对程序作出解释(这一点和注释 ...
- 【漏洞复现】Apache RocketMQ 代码注入漏洞(CVE-2023-37582)
产品介绍 Apache RocketMQ是美国阿帕奇(Apache)基金会的一款轻量级的数据处理平台和消息传递引擎. 漏洞概述 Apache RocketMQ 存在代码注入漏洞,该漏洞源于当 Name ...
- Java 创建/识别条形码
项目刚好需要用到就记录一下 -- 依赖 <!-- 条形码生成 --><dependency> <groupId>net.sf.barcode4j</group ...
- jquery加载页面时触发事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 微服务实践k8s&dapr开发部署实验(3)订阅发布
自托管模式运行dapr 新建订阅webapi项目,取名为backend 项目增加docker支持,取消https支持 修改Program.cs var builder = WebApplication ...
- 微服务新体验之Aspire初体验
安装aspire 查看vs版本 我这的版本是17.9.7,不支持aspire,所以需要升级 更新VS 点击 帮助->检查更新 点击更新 静等安装升级 创建aspire项目 项目创建成功,如下图 ...
- 使用jasypt对springboot配置信息加密
1.pom文件增加依赖 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactI ...
- Lru在Rust中的实现, 源码解析
LRU(Least Recently Used)是一种常用的页面置换算法,其核心思想是选择最近最久未使用的页面予以淘汰. LRU算法原理 基本思想:LRU算法基于一个假设,即如果一个数据在最近一段时间 ...
- cookie cookie的获取
什么是 cookie cookie 是 浏览器 的 一种功能 是 浏览器 用来 存储 前端数据的一种 存储机制 ...