【本文连接】

http://www.cnblogs.com/hellogiser/p/user_initialization_list.html

【分析】

初始化列表和构造函数内的赋值语句有何区别?

(1)对于built-in数据类型,如int long,指针等,初始化列表和构造函数内的赋值语句效果和效率一样。

(2)对于user-defined数据类型,比如class对象等,初始化列表和构造函数内的赋值语句效果是一样的,但是效率却有很大的差异。

如果一个Class1内部有Class2的一个对象Class2 obj,并且通过Class2 &robj来初始化,那么

(2.1)对于执行初始化列表的obj(robj)部分,只会调用Class2的copy constructor

(2.2)对于构造函数内的赋值obj = robj语句,在执行构造函数体之前,会调用Class2的default constructor来初始化obj,然后执行obj = robj语句,调用Class2的copy assignment。

也就是说,初始化列表比构造函数内赋值要更高效。

【const数据成员】

对于Class的const或者ref数据成员,只能通过初始化列表初始化,不能够赋值。

【代码1】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

class Food
{
public:
    Food(): m_v(v)
    {
        puts("default consturctor");
    }

Food(const Food &other)
    {
        puts("copy consturctor");
        this->m_v = other.m_v;
    }

Food &operator =(const Food &other)
    {
        puts("copy assignment");
        if(this == &other)
            return *this;
        this->m_v = other.m_v;
        return *this;
    }
private:
    int m_v;
};

class Dog
{
public:
    /*
    for User Intialization List
    only copy constructor was called
     * */
    Dog()
    {
        cout << weight << endl; // 100
    }
private:
    int weight;
    const int LEGS;
    Food food;
};

void test_case1()
{
    Food f;
    Dog d(, f);
    puts("-----------------");
}

int main()
{
    test_case1();
    ;
}

/*
 * default consturcor
 * copy constructor
 * 111
 * */

【代码2】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

class Food
{
public:
    Food(): m_v(v)
    {
        puts("default consturctor");
    }

Food(const Food &other)
    {
        puts("copy consturctor");
        this->m_v = other.m_v;
    }

Food &operator =(const Food &other)
    {
        puts("copy assignment");
        if(this == &other)
            return *this;
        this->m_v = other.m_v;
        return *this;
    }
private:
    int m_v;
};

class Dog2
{
public:
    /*
    for assignment inside {} of constructor
    default constructor + copy assignment were called
     * */
    Dog2()
    {
        cout << weight << endl; // -3174682  (has not initialized so a random number)
        weight = w;
        food = f;
    }
private:
    int weight;
    const int LEGS;
    Food food;
};

void test_case2()
{
    Food f;
    Dog2 d(, f);
    puts("-----------------");
}

int main()
{
    test_case2();
    ;
}

/*
 * default constructor
 * default construcotr
 * -876545120
 * copy assignment
 * */

user initialization list vs constructor assignment的更多相关文章

  1. Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization

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

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

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

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization & Cleanup

    Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> ru ...

  4. 深入探索c++对象模型

    第一章关于对象 c++在布局和存取时间的额外负担主要有virtual引起 virtual function:运行期动态绑定 virtual base class :base class多次出现在派生类 ...

  5. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  6. C++ Copy Elision

    故事得从 copy/move constructor 说起: The default constructor (12.1), copy constructor and copy assignment ...

  7. iOS:消除项目中警告

    引言: 在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能执行之类的警告. 有代码洁癖的孩子们很想消除他们, 今天就让我们来一次Fuck 警告!! 首先学会基本的语句: ...

  8. 【转】clang warning 警告清单(备查,建议直接command + F 速查 )

    Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belon ...

  9. C++ QUICK REFERENCE

    C++ string 用法详解 字符串分割(C++)  C++ QUICK REFERENCE Matt Mahoney, mmahoney@cs.fit.edu DECLARATIONS enum ...

随机推荐

  1. 屏蔽防止被别的网站嵌入框架代码(防止被人frame)

    <SCRIPT LANGUAGE=javascript> if (top.location != self.location)top.location=self.location; < ...

  2. 在iOS APP中使用H5显示百度地图时如何支持HTTPS?

    现象: 公司正在开发一个iOSAPP,使用h5显示百度地图,但是发现同样的H5页面,在安卓可以显示出来,在iOS中就显示不出来. 原因分析: 但是现在iOS开发中,苹果已经要求在APP中的所有对外连接 ...

  3. Caliburn.Micro学习笔记(一)----引导类和命名匹配规则

    Caliburn.Micro学习笔记目录 用了几天时间看了一下开源框架Caliburn.Micro 这是他源码的地址http://caliburnmicro.codeplex.com/ 文档也写的很详 ...

  4. Windows操作系统组策略应用全攻略

    作者:佚名出处:IT专家网论坛2007-07-23 13:31 一.什么是组策略 (一)组策略有什么用? 说到组策略,就不得不提注册表.注册表是Windows系统中保存系统.应用软件配置的数据库,随着 ...

  5. Spring+EhCache缓存实例

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

  6. eclipse发布web项目到生产环境的方式汇总(tomcat)

    参考: http://www.cnblogs.com/xiohao/p/3689832.html http://www.111cn.net/jsp/J2EE-EJB/90337.htm 我收集了几点: ...

  7. 防DDOS攻击SHELL脚本

    最近一段时间服务器频繁遭到DDOS攻击,目前只能通过封IP来源来暂时解决.IP不源变化多端,光靠手工来添加简直是恶梦,想了个方法,用SHELL来做. 比较简单,但很实用:) 以下内容根据作者原文进行适 ...

  8. 【Alpha版本】十天冲刺——日志集合贴

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 Day1 Day2 Day3 Day ...

  9. RabbitMQ消息队列(一): Detailed Introduction 详细介绍

     http://blog.csdn.net/anzhsoft/article/details/19563091 RabbitMQ消息队列(一): Detailed Introduction 详细介绍 ...

  10. 触发器运用示例---laobai

    1 触发器 概念:trigger.逻辑对象的一种.当dml的增删改语句执行时,自动触发一系列动作. 分类:dml触发器.ddl触发器(很少见) sql:ddl,dml,dcl 按触发的时间分: 语句执 ...