zoukankan      html  css  js  c++  java
  • 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:函数只能被定义一次,要么在函数内,要么在函数外。使用初始化列表初始化时必须定义函数!

  • 相关阅读:
    MYSQL存储引擎
    微信公众号自定义菜单
    TCP 三次握手与四次挥手
    微信扫码关注公众号并登录网站
    redis scan命令使用
    [转]Maven多模块结构下版本管理的正确姿势-revision
    线程间的协作wait,notify,sleep,yield,join
    GIT 撤销操作
    Kafka学习理解-listeners配置
    Kafka 简介梳理
  • 原文地址:https://www.cnblogs.com/XiaoXiaoShuai-/p/11498823.html
Copyright © 2011-2022 走看看