【本文连接】

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. CSS基本知识1-CSS基本概念

    CSS基本概念: 选择器{属性:值;属性:值} CSS继承:子元素继承父元素样式,父子关系看DOM结构. CSS覆盖: 浏览器缺省设置 外部样式表 内部样式表(位于 <head> 标签内部 ...

  2. Jumony Core 3,真正的HTML引擎,正式版发布

    Jumony是一个开源项目,已经有三年的历史了,在这三年中,秉承提供给.NET程序员完整的HTML掌控能力,Jumony历经无数次的改进,终于进入了一个新的阶段.Jumony Core 3是一个真正意 ...

  3. Splay

    #include <cstdio> #include <iostream> using namespace std; *1e5;//nil表示不存在的节点 ][],flag[] ...

  4. java8日期时间

    ###与时间有关的5个包* java.time* java.time.chrono* java.time.format* java.time.temporal* java.time.zone###ja ...

  5. 1122Shell脚本之利用mysqldump备份MySQL数据库

    #!/bin/bash #Mysql 自动备份 压缩并上传到 指定ftp #设想每天凌晨3点备份mysql #编辑crontab配置文件 #00 03 * * * backupmysql.sh #压缩 ...

  6. Linux--装好之后要做的几件事(转)

    1.删除libreoffice libreoffice虽然是开源的,但是Java写出来的office执行效率实在不敢恭维,装完系统后果断删掉 sudo apt-get remove libreoffi ...

  7. 【转】C# lock的使用

    一.Lock定义     lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.它可以把一段代码定义为互斥段(critical section),互斥段在一个时刻内只允许一个线程进入执行, ...

  8. Hibernate JPA实体继承的映射(一) 概述

    http://www.cnblogs.com/yingsong/p/5179975.html   注:文章中据说的实体指的是被@Entity注解的类. JPA中对象关系映射通常情况下是一个实体对应一个 ...

  9. BZOJ 4568 幸运数字

    题目传送门 4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MB Description A 国共有 n 座城市,这些城市由 n-1 ...

  10. <<< sqlserver、Mysql、Oracle数据库优缺点

    sqlserver 优点: 易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能.与许多其他服务器软件紧密关联的集成性.良好的性价比等:   为数据管理与分析带来了灵活性,允许单位在快速变化的 ...