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的更多相关文章

  1. __attribute__中constructor和destructor

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  2. Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization

    w https://zh.wikipedia.org/wiki/RAII RAII要求,资源的有效期与持有资源的对象的生命期严格绑定,即由对象的构造函数完成资源的分配(获取),同时由析构函数完成资源的 ...

  3. __attribute__中constructor和destructor[总结]

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  4. Constructor and destructor -- Initialization & Cleanup in C++

    Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...

  5. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  6. C++ 类 复制构造函数 The Copy Constructor

    一.复制构造函数的定义 复制构造函数是一种特殊的构造函数,具有一般构造函数的所有特性.复制构造函数创建一个新的对象,作为另一个对象的拷贝.复制构造函数只含有一个形参,而且其形参为本类对象的引用.复制构 ...

  7. 深度探索C++对象模型之第二章:构造函数语意学之Default constructor的构造操作

    C++新手一般由两个常见的误解: 如果任何class没有定义默认构造函数(default constructor),编译器就会合成一个来. 编译器合成的的default constructor会显示的 ...

  8. TAQSkinScrollBar 类美化滚动条再讨论

    再说:TAQSkinScrollBar 类美化滚动条,http://www.138soft.com/?p=156  里面有人提到不可以滚动 滚动的改善方法: unit AQSkinScrollBar; ...

  9. Delphi 关键字详解[整理于 "橙子" 的帖子]

    absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var   Str: ];   StrLen: Byte absolute Str; //这个声明指定了变量 ...

  10. TLogger一个D7可用的轻量级日志

    今天调程序,要用到日志.XE7有Qlog,D7用什么 从网上找到了Logger,下载的原文是不支持D7的,不过也只是很少的地方不同,自己修改了下就可以用了 感谢原作者和红鱼的分享 unit Logge ...

随机推荐

  1. 通过 OpenAPI 部署 Nbsf_Management API Service

    目录 文章目录 目录 准备 部署 启动 API 服务 调用 准备 GentOS7 Golang1.12.5 Swagger YAML TS29521_Nbsf_Management.yaml TS29 ...

  2. ETSI GS MEC 015,MEP 带宽管理 API

    目录 文章目录 目录 版本 ETSI MEC 对 MEP 带宽管理功能的定义 功能理解 Bandwidth Management Service BWMS UML ME APP registers t ...

  3. Python基础篇(流程控制)

    流程控制是程序运行的基础,流程控制决定了程序按照什么样的方式执行. 条件语句 条件语句一般用来判断给定的条件是否成立,根据结果来执行不同的代码,也就是说,有了条件语句,才可以根据不同的情况做不同的事, ...

  4. winform 绘图控件 chart 实时曲线图

    官方教程:http:////files.cnblogs.com/files/HelloQLQ/Winform图表.rar 更多参考:https://blog.csdn.net/boxuming/art ...

  5. ASP.NET Core、Winform、WPF 删除多余的Microsoft.CodeAnalysis语言资源文件

    摘要:ASP.NET Core 3.1网站生成项目时,输出文件夹多出很多Microsoft.CodeAnalysis的语言资源文件github issue 问题# ASP.NET Core3.1网站生 ...

  6. ubuntu18.04开机grub引导界面、登录界面美化

    1.引导界面美化 下载grub主题 https://www.gnome-look.org/browse/cat/109/order/latest/ https://www.pling.com/s/Gn ...

  7. NOIP模拟53

    我在时光斑驳深处,聆听到花开的声音. 前言 这套题好像是随便拼接起来的,来自三套不同的题,最后一道还是学长出的(nb 场上为数不多的几次死磕一道题正解,大概有三个小时吧(惭愧,前两个小时看错题了,一直 ...

  8. c#笔记(3) 委托回调

    委托回调是刚接触c#时最头疼的东西,老看老忘,遂整理一下现在对委托回调的理解.如有错误,请指出,感谢. 委托 C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托是存有对某个方 ...

  9. vue3拉取代码install 报错 npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: npm ERR! Found: vue@3.2.31

    先看报错 说明安装的包和现有的包已经冲突了版本不一致 我们先试一下npm install @vue/cli -- force然后再试一下npm install @vue/cli --legacy-pe ...

  10. LeetCode 128. Longest Consecutive Sequence 最长连续序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...