c++利用初始化列表在类内部和类外部定义构造函数的区别
case 1:在类外定义构造函数,所有data member都在初始化列表中进行初始化。
class SupportClass
{
public:
SupportClass()
{
cout << "SupportClass() is called" << endl;
}
SupportClass(SupportClass&)
{
cout << "SupportClass(SupportClass&) is called" << endl;
}
~SupportClass()
{
cout << "~SupportClass() is called" << endl;
}
SupportClass& operator=(SupportClass&)
{
cout << "SupportClass()& operator=(SupportClass&) is called" << endl;
return *this;
}
}; class TestInitialize
{
public:
TestInitialize(SupportClass arg);
private:
SupportClass data;
}; TestInitialize::TestInitialize(SupportClass arg) :data(arg){} int main()
{
SupportClass sc;
cout << endl;
TestInitialize ti(sc);
return 0;
}
结果:

第一次默认构造:来自main函数中定义的变量sc;
第二次拷贝构造:来自形参拷贝实参;
第三次拷贝构造:来自data member 拷贝形参。
结论:与类内定义一样。
case 2:在类外定义构造函数,初始化列表为空,data member 在函数体中定义。
TestInitialize::TestInitialize(SupportClass arg) { data = arg; }
结果;

第一次默认构造:来自main函数中定义的变量sc;
第二次拷贝构造:来自形参拷贝实参;
第三次默认构造:来自data member 的默认初始化。
接着的 operator=:data 复制 形参。
结论:与类内定义一样。
case 3:类有多个 data 成员,全用初始化列表初始化,函数体为空
添加一个class:AnotherClass
class AnotherSupportClass
{
public:
AnotherSupportClass()
{
cout << "AnotherSupportClass() is called" << endl;
}
AnotherSupportClass(AnotherSupportClass&)
{
cout << "AnotherSupportClass(AnotherSupportClass&) is called" << endl;
}
~AnotherSupportClass()
{
cout << "~AnotherSupportClass() is called" << endl;
}
AnotherSupportClass& operator=(AnotherSupportClass&)
{
cout << "AnotherSupportClass()& operator=(AnotherSupportClass&) is called" << endl;
return *this;
}
};
TestInitialize::TestInitialize(SupportClass arg1, AnotherSupportClass arg2)
:data(arg1), anotherData(arg2){}
结果:

意外发现:
我的构造函数的参数列表是:先SupportClass,然后再AnotherSupportClass。
结果在由实参拷贝到形参时时:先AnotherSupportClass,再SupportClass。
又做了一个实验:再添加一个成员
class TestInitialize
{
public:
TestInitialize(SupportClass arg1, AnotherSupportClass arg2,OtherSupportClass arg3);
private:
SupportClass data;
AnotherSupportClass anotherData;
OtherSupportClass otherData;
}; TestInitialize::TestInitialize(SupportClass arg1, AnotherSupportClass arg2, OtherSupportClass arg3)
:data(arg1), anotherData(arg2),otherData(arg3){};

依旧是逆序拷贝构造的,重点是我不信邪,循环了100次,都是这个结果。
结论:函数参数列表的拷贝是随机的,并不是按照参数出现的先后顺序。
case 4:函数只能被定义一次,要么在函数内,要么在函数外。使用初始化列表初始化时必须定义函数!
c++利用初始化列表在类内部和类外部定义构造函数的区别的更多相关文章
- Linux shell 内部命令与外部命令有什么区别以及怎么辨别
内部命令实际上是shell程序的一部分,其中包含的是一些比较简单的linux系统命令,这些命令由shell程序识别并在shell程序内部完成运行,通常在linux系统加载运行时shell就被加载并驻留 ...
- 4.给定一个正整数m,统计m的位数,分别打印每一位数字,再按照逆序打印出各位数字。 要求:m定义为类的属性,需定义构造函数为m赋值;当m大于99999时,输出错误信息“the number is too large”,不再执行。
package a; public class ShuZi { int m; public int getM() { return m; } public void setM(int m) { thi ...
- c++ 关于类构造函数的初始化列表
除了性能问题之外,有些时场合初始化列表是不可或缺的,以下几种情况时必须使用初始化列表 常量成员,因为常量只能初始化不能赋值,所以必须放在初始化列表里面 引用类型,引用必须在定义的时候初始化,并且不能重 ...
- C++类够做函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式.例如: class CExample{ public: int a; float b; C ...
- C++学习之路(十一):C++的初始化列表
结论: 1.在C++中,成员变量的初始化顺序与变量在类型中的声明顺序相同,而与他们在构造函数的初始化列表中的顺序无关. 2.构造函数分为两个阶段执行:1)初始化阶段:2)普通的计算阶段,表现为赋值操作 ...
- c++,初始化列表
类对象的构造顺序是这样的: a.分配内存,调用构造函数时,隐式/显示的初始化各数据成员 b.进入构造函数后在构造函数中执行一般计算 1.初始化类的成员有两种方式,一是使用初始化列表,二是在构造函数体内 ...
- C++ 初始化列表(转)
转载自:http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html 何谓初始化列表 与其他函数不同,构造函数除了有名字,参数列表和函 ...
- C++初始化列表(good)
本文转载自http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html 感谢作者分享 何谓初始化列表 与其他函数不同,构造函数除了有名 ...
- 【校招面试 之 C/C++】第1题 为什么优先使用构造函数的初始化列表
1.首先看一个例子: #include<iostream> using namespace std; class Test1 { public: Test1() // 无参构造函数 { c ...
随机推荐
- 【转】SQL语句面试题
1.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2 ...
- hdu6621 二分加主席树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 Problem Description You have an array: a1, a2, ...
- 我终于学会了使用python操作postgresql
一 前言 这篇文章不仅适合pgsql,更适合mysql,思路都是一致的,如果读者学会使用psycopg2操作pgsql,那么使用PyMySQL 操作mysql也是很简单:本篇文章涵盖内容广泛,提供的操 ...
- Django 数据库连接缓存的坑
https://www.cnblogs.com/xcsg/p/11446990.html
- Bootstrap 常用网站
https://www.bootcss.com/ 中文官方文档 https://www.bootcdn.cn/ BootCDN http://www.fontawesome.com.cn/ ...
- 从0开发3D引擎(七):学习Reason语言
目录 上一篇博文 介绍Reason Reason的优势 如何学习Reason? 介绍Reason的部分知识点 大家好,本文介绍Reason语言以及学习Reason的方法. 上一篇博文 从0开发3D引擎 ...
- 痞子衡嵌入式:语音处理工具pzh-speech诞生记(2)- 界面构建(wxFormBuilder3.8.0)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是语音处理工具pzh-py-speech诞生之界面构建. 之前痞子衡设计过一个串口调试助手pzh-py-com,也专门写过一篇关于其界面构 ...
- python接口自动化测试 - openpyxl封装类
前言 为了更好的让openpyxl在工作中使用,将openpyxl的常用操作封装起来,这样不仅复用性高,而且阅读性好 直接上代码 #!/usr/bin/env python # -*- coding: ...
- linux 的21个命令:
1:ls [list] 2:输入输出重定向 3:| 管道 4:chmod 5:cd 6:mkdir 7:cp 8:rm 9:mv 10:cat 11:pwd 12:ln[link] 13:grep ...
- lind 语 api 数据的安全性 第四弹
web api的安全性怎么保证呢. 一般公司会自己封装一套请求的规范. 下面来看看lind语里的webapi安全规范 step one 先看下 diagram: 学而思: 从上面的图分析一下: 如果我 ...