user initialization list vs constructor assignment
【本文连接】
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(int v = 10): 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(int w, Food &f): weight(w), food(f), LEGS(4) { cout << weight << endl; // 100 } private: int weight; const int LEGS; Food food; }; void test_case1() { Food f; Dog d(111, f); puts("-----------------"); } int main() { test_case1(); return 0; } /* * 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(int v = 10): 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(int w, Food &f): LEGS(4) { 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(99, f); puts("-----------------"); } int main() { test_case2(); return 0; } /* * default constructor * default construcotr * -876545120 * copy assignment * */ |