委派构造函数可以减少构造函数的书写量:

class Info {
public:
Info() : type(), name('a') {
InitRest();
} Info(int i) : type(i), name('a') {
InitRest();
} Info(char e) : type(),, name(e) {
InitRest();
} private:
void InitRest() { //其他初始化 }
int type;
char name;
};

每个构造函数都需要初始化列表来初始化成员type和name,且都调用了相同的函数InitRest,存在重复。

而在C++11中,可以使用委派构造函数来简化代码,如下:

class Info {
public:
Info() { InitRest(); } //称为目标构造函数(被调用)
Info(int i) : Info() { type = i; } //委派构造函数(调用者)
Info(char e) : Info() { name = e; } private:
void InitRest() { // other int }
int type {};
char name {'a'};
};

委派构造函数只能在函数体内为type,name等成员赋值,因为委派构造函数不能有初始化列表。C++中,构造函数不能同时使用委派和初始化列表。

初始化列表的初始化总是先于构造函数完成,作一下修改:

class Info {
public:
Info() : Info(, 'a') {}
Info(int i) : Info(i, 'a') {}
Info(char e) : Info(, e) {} private:
Info(int i, char e) : type(i), name(e) {} //将初始化动作放在一个通用的构造函数里
int type;
char name;
};

在构造函数比较多时,可以有不止一个委派构造函数,目标函数也可以是委派构造函数,可以在委派构造函数中形成链状的委派构造关系。

class Info {
public:
Info() : Info() {}
Info(int i) : Info(i, 'a') {} //可以委派成链状,但不能形成环。
Info(char e) : Info(, e) {} private:
Info(int i, char e) : type(i), name(e) {}
int type;
char name;
};

委派构造函数的应用:

1.模板:

#include <list>
#include <vector>
#include <deque> using namespace std; class TDConstructed {
template<class T>
TDConstructed(T first, T last) : l(first, last) {} list<int> l; public:
TDConstructed(vector<short> & v) : TDConstructed(v.begin(), v.end()) {}
TDConstructed(deque<int> & d) : TDConstructed(d.begin(), d.end()) {}
}

以上定义了一个构造函数模板,通过两个委派构造函数的委托,构造函数模板被实例化。

2. 异常处理:

#include <iostream>
using namespace std; class DCExcept(double d) try : DCExcept(, d) {
cout << "Run the body." << endl;
} catch(...) {
cout << "caught exception." << endl;
} private:
DCExcept(int i, double d) {
cout << "going to throw!" << endl;
throw ;
} int type;
double data;
}; int main() {
DCExcept a(1.2);
}

目标函数中抛出异常,可以在委派构造函数中捕获。

c++11 委派构造函数的更多相关文章

  1. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  2. C++11:移动构造函数的测试

    C++11:移动构造函数的测试 代码如下: #include <iostream> #include <stddef.h> #include <Windows.h> ...

  3. C# DateTime的11种构造函数

    别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Globalization; using Syste ...

  4. c++11 move构造函数和move operator 函数 学习

    先看个代码吧!!!!!!!!!! #include <iostream> using namespace std; class A { public: A(){cout<<&q ...

  5. [C++11] 默认构造函数

    类通过一个特殊的构造函数来控制默认初始化过程.这个函数就是默认构造函数.默认构造函数无需不论什么实參. 我们能够显示的定义默认构造函数也能够让编译器为我们生成默认构造函数. 默认构造函数以例如以下规则 ...

  6. c++11的构造函数继承

    https://en.cppreference.com/w/cpp/language/using_declaration 在[Inheriting constructors]这一节. 其实叫做"基类的 ...

  7. c++11 继承构造函数

    若基类拥有数量众多的不同版本的构造函数,而派生类中只有一些成员函数,则对于派生类而言,其构造函数就等同于构造基类. struct A { A(int i) {} A(double d, int i) ...

  8. C++11实现模板手柄:委托构造函数、defaultkeyword分析

    C++11.使用委托构造函数.和高速变量初始化,defaultkeyword重新声明默认构造函数,回答pod状态. 分析与推荐的方法. 到目前为止,VS2012和2013异常声明兼容还是停留在通信代码 ...

  9. C++11 构造函数的改动

    一.继承构造函数 继承构造函数的引入原因:如果基类的构造函数很多,那么子类的构造函数想要实现同样多的构造接口,必须一一调用基类的构造函数,有点麻烦. 于是乎:C++11引入继承构造函数,子类可以通过使 ...

随机推荐

  1. 前端必用正则(js)

    手机号 /^1((3[\d])|(4[5,6,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[1-3,5-8])|(9[1,8,9]))\d{8}$/ 大写字母 /^[A- ...

  2. 【Mybatis】Mybatis缓存

    mybatis提供了缓存机制减轻数据库压力,提高数据库性能 mybatis的缓存分为两级:一级缓存.二级缓存 一级缓存是SqlSession级别的缓存,缓存的数据只在SqlSession内有效 二级缓 ...

  3. MySQL/RDS数据如何同步到MaxCompute之实践讲解

    摘要:大数据计算服务(MaxCompute,原名ODPS)是阿里云提供的一种快速.完全托管的EB级数据仓库解决方案.本文章中阿里云MaxCompute公有云技术支持人员刘力夺通过一个实验向大家介绍了阿 ...

  4. C#读取csv、xls、sql数据库的实现

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  5. (65)C# 任务

    1.启动任务 //Framework4.5新增的Task.Run开启一个任务,Run方法中传入一个Action委托 Task.Run(()=> { Thread.Sleep(); Console ...

  6. TypeScript躬行记(5)——类型兼容性

    TypeScript是一种基于结构类型的语言,可根据其成员来描述类型.以结构相同的Person接口和Programmer类为例,如下所示. interface Person { name: strin ...

  7. sql server中实现mysql的find_in_set函数和group_concat类似功能的讲解

    charindex(','   +  ' test '+  ','   ,   ',' + test2+ ',')>0 灵活运用 SQL SERVER FOR XML PATH FOR XML ...

  8. delphi 简单的发送字符串消息

    var pMes:^String; begin New(pMes); pMes^:=msg; PostMessage(Application.handle, WM_Custom, 0, Integer ...

  9. zabbix部署agent

    1.下载zabbix源 rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-2.el7.noarc ...

  10. 【C#学习笔记】 IDisposable 接口

    在.net 编程环境中,系统的资源分为托管资源和非托管资源. 对于托管的资源的回收工作,是不需要人工干预回收的,而且你也无法干预他们的回收,所能够做的只是了解.net CLR如何做这些操作.也就是说对 ...