【本文连接】

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. 基本排序(二)插入排序(直接插入、Shell、折半)

    插入排序是常见的内部排序之一.常见的插入排序包括直接插入排序.Shell排序.折半排序.本篇主要介绍这三个排序. 转载请注明出处——http://www.cnblogs.com/zrtqsk/p/38 ...

  2. rpc框架之 thrift连接池实现

    接前一篇rpc框架之HA/负载均衡构架设计 继续,写了一个简单的thrift 连接池: 先做点准备工作: package yjmyzz; public class ServerInfo { publi ...

  3. 微信小程序入门正确姿势(一)

    [未经作者本人同意,请勿以任何形式转载] >>>前言 这是 [认真学编程] 系列的 第4篇 文章(微信小程序入门系列),欢迎点赞分享.写留言,这些都是对我最好的支持. 本系列适合有一 ...

  4. LintCode-Longest Increasing Subsequence

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  5. 分形之概率学下的green tree

         今天做的是分形之随机概率,可以和以前做的那个抛色子的做法非常相似,抛色子是用随机点数控制图形,今天做的树叶图形只是用概率的做法去控制图形而已,做法是如出一辙的: //图形界面 package ...

  6. 作业一:android开发平台的演变以及Android Studio设置

    目录:     ①. 从Eclipse到Android Studio   ②. Android Studio的下载和安装   ③. 用户习惯设置以及快捷键   ④. SDK路径重新设置 ↓点此跳转到文 ...

  7. Jsoup 使用教程:输入

    使用背景: 使用网络爬虫(或者手动复制),从别的网站上下载下来的内容,都是一堆的html,很多标签.样式 等等都可能是你所不需要的,或者 想要变成你想要的样式.那么该怎么办呢? 我们知道,每一个网页都 ...

  8. Advanced Office Password Recovery如何设置快捷方式

    一般软件安装成功之后都会在桌面上形成快捷方式以方便使用,但是一些用户发现Advanced Office Password Recovery这种office密码破解工具安装成功后桌面上没有出现快捷方式, ...

  9. 理解Cookie和Session机制(转)

    目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...

  10. 初识Jsoup之解析HTML

    按照国际惯例,我首先应该介绍下Jsoup是个什么东西,然后在介绍下具体用法,然后在来个demo演示,其实我也是这么想的,小编今天花了一天的时间从学习—>解析页面,总算是成果圆满了吧,啊哈,但是, ...