【本文连接】

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. Spring源码分析——BeanFactory体系之接口详细分析

    Spring的BeanFactory的继承体系堪称经典.这是众所周知的!作为Java程序员,不能错过! 前面的博文分析了Spring的Resource资源类Resouce.今天开始分析Spring的I ...

  2. 让所有的浏览器都能识别HTML5标签样式的小插件

    如今HTML5愈来愈引发大家的关注了,但目前支持HTML5的浏览器还不是主流,特别是国内用户近50%以上仍旧使用IE6,由于支持HTML5的IE9不支持Xp系统安装,这样未来很长一段时间,HTML5的 ...

  3. Win10升级后回退后无法检测新版本的修复办法

    笔记本原来装的是Win10 10240版本,升级到14393版本后进行了回退.回退后,Win10系统再也检测不到新版本更新了.   解决办法如下: 1.打开注册表:HKEY_LOCAL_MACHINE ...

  4. VMware三种上网模型

    今天捣鼓了一会虚拟机,对上网方式又学习了一遍,之前摆弄过,现在又捡起来了,主要自己整理一下,方面后面复习.主要有三种网络模型:桥接.仅主机(Host-Only).NAT.自己亲测了这三种方式,都可以上 ...

  5. PHP 常用框架

    1.ThinkPHP 2.Yii2 3.Laravel 4.CodeIgniter 5.CakePHP

  6. 使用属性动画 — Property Animation

    属性动画,就是通过控制对象中的属性值产生的动画.属性动画是目前最高级的2D动画系统. 在API Level 11中添加.Property Animation号称能控制一切对象的动画,包括可见的和不可见 ...

  7. git远程仓库

    git远程仓库: 目前我们使用到的 Git 命令都是在本地执行,如果你想通过 Git 分享你的代码或者与其他开发人员合作. 你就需要将数据放到一台其他开发人员能够连接的服务器上. 添加远程仓库: gi ...

  8. Django登录访问限制 login_requeired

    作用: 1. 用户登录之后才可以访问某些页面 2. 如果没登录,跳转到登录页面 3. 用户在跳转的登陆界面中完成登陆后,自动访问跳转到之前访问的地址 要实现这个需求很简单就是在相应的view前面使用装 ...

  9. 【UOJ #246】【UER #7】套路

    http://uoj.ac/contest/35/problem/246 神奇!我这辈子是想不出这样的算法了. 对区间长度分类讨论:题解很好的~ 我已经弱到爆了,看完题解后还想了一晚上. 题解中&qu ...

  10. C++ URLDecode和URLEncode实现——仅限gb2312,非utf8

    转载--http://blog.163.com/zhangjie_0303/blog/static/9908270620148251658993/   #include <iostream> ...