【本文连接】

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. canvas弹动效果

    弹动效果,用物体与目标的距离乘上系数再累加至速度上,让物体呈加速度运动,再让速度乘与摩擦力系数,让物体最终停止运动 代码如下所示 var canvas = document.getElementByI ...

  2. python使用uuid库生成唯一id

    概述: UUID是128位的全局唯一标识符,通常由32字节的字符串表示. 它可以保证时间和空间的唯一性,也称为GUID,全称为: UUID -- Universally Unique IDentifi ...

  3. 前端代码目录结构、常用 piugin、元素补充用法及其它注意事项

    目录结构: app:  .html文件 css: .css文件 script: 脚本文件 plugin: 插件  (此目录放一些通用代码) 注意事项: 1.在IE浏览器下img会显示边框,为了保证兼容 ...

  4. 自创yum源

    这里我们用U盘装完系统以后,我们要是使用yum命令的话,我们是装不上包的 这个时候我们装系统的时候 [root@kz-002 Packages]# df -h Filesystem Size Used ...

  5. 红米3 TWRP-3.0.2(android_6.0.1_r72分支)中文版Recovery更新于20161018

    TWRP3.0.2更新简介 TWRP是TeamWin团队https://github.com/TeamWin/Team-Win-Recovery-Project的开源项目,也是Omnirom系统默认的 ...

  6. BZOJ 2120: 数颜色

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 3623  Solved: 1396[Submit][Status][Discuss] ...

  7. Leetcode 230. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  8. 关于redis的keys命令的性能问题

    KEYS pattern 查找所有符合给定模式 pattern 的 key . KEYS * 匹配数据库中所有 key . KEYS h?llo 匹配 hello , hallo 和 hxllo 等. ...

  9. CSS命名规范

    DIV+CSS规范命名大全集合 前端人员必看CSS命名规范 整理: 文件名必须由小写字母.数字.中划线组成 ).所有的命名最好都小写,一律采用小写加中划线的方式,不允许使用大写字母或 _2).属性的值 ...

  10. 团队博客作业- Week3

    成员介绍 韩青长  测试 我是韩青长,技术小白,抱着对软工的好奇和对未来工作的憧憬选了这门课.暂时选择了测试的工作,也对开发和UI有一定兴趣.从前上帝创造了我们,现在轮到我们来创造自己的软件了~ 陈彦 ...