• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Zero Lee的專欄

            構造函數,拷貝構造,拷貝賦值

            Example1:
             1 class A {
             2 public:
             3     A() {
             4         std::cout << "A.ctor()" << std::endl;
             5     }
             6     A(const A& other) {
             7         std::cout << "A.copyctor()" << std::endl;
             8     }
             9     A& operator =(const A& other) {
            10         std::cout << "A.op =() " << std::endl;
            11     }
            12 };
            13 
            14 class AA : public A
            15 {
            16 public:
            17     AA() {
            18         std::cout << "AA.ctor()" << std::endl;
            19     }
            20 };
            21 
            22 int main()
            23 {
            24     AA aa;  // A.ctor => AA.ctor
            25     AA bb(aa);  // A.copyctor
            26     aa = bb;    // A.op =
            27     return 0;
            28 }
            29 
            1. 編譯器會默認調用基類的構造函數。
            2. 繼承類的拷貝構造函數/拷貝賦值運算符函數沒有定義,編譯器會默認調用基類相應的函數。

            Example2:
             1 class A {
             2 public:
             3     A() {
             4         std::cout << "A.ctor()" << std::endl;
             5     }
             6     A(const A& other) {
             7         std::cout << "A.copyctor()" << std::endl;
             8     }
             9     A& operator =(const A& other) {
            10         std::cout << "A.op =() " << std::endl;
            11     }
            12 };
            13 
            14 class AA : public A
            15 {
            16 public:
            17     AA() {
            18         std::cout << "AA.ctor()" << std::endl;
            19     }
            20     AA(const AA& other)
            21     : A(other) {
            22         std::cout << "AA.copyctor() " << std::endl;
            23     }
            24     AA& operator =(const AA& other) {
            25         std::cout << "AA.op =()" << std::endl;
            26     }
            27 };
            28 
            29 int main()
            30 {
            31     AA aa;  // A.ctor => AA.ctor
            32     AA bb(aa);  // A.copyctor => AA.copyctor
            33     aa = bb;    // AA.op =
            34                                 
            35     return 0;
            36 }
            1. 拷貝構造函數會默認調用基類的構造函數,而不是對應的拷貝構造函數,除非在自己手動調用。
            2. 自定義的拷貝賦值運算符函數,也不會調用基類的相應函數。


            posted on 2010-12-03 15:21 Zero Lee 閱讀(262) 評論(0)  編輯 收藏 引用 所屬分類: CC++ Programming

            日韩中文久久| 蜜桃麻豆WWW久久囤产精品| 久久99精品久久久久久动态图 | 国内精品久久久久久麻豆| 久久99精品国产麻豆婷婷| 99久久精品免费看国产一区二区三区| 麻豆亚洲AV永久无码精品久久| 伊人久久综在合线亚洲2019 | 久久无码AV一区二区三区| 久久婷婷国产综合精品| 久久精品视频91| 久久亚洲精品成人av无码网站| 久久久久久青草大香综合精品 | 国产午夜久久影院| 久久精品亚洲AV久久久无码| 久久婷婷国产麻豆91天堂| 久久国内免费视频| 国产精品热久久毛片| 国产99久久精品一区二区| 久久只有这里有精品4| 国内精品久久国产大陆| 亚洲精品乱码久久久久久| 久久亚洲精品无码播放| 精品99久久aaa一级毛片| 精品久久久久香蕉网| 色诱久久久久综合网ywww | 久久综合偷偷噜噜噜色| 久久99国产精品成人欧美| 久久91亚洲人成电影网站| 国产成人无码久久久精品一| 99久久夜色精品国产网站 | 久久亚洲精精品中文字幕| 久久久久国产精品人妻| 久久99国产精品久久99小说| 欧美久久天天综合香蕉伊| 久久久久亚洲av成人无码电影 | 狠狠色丁香婷婷久久综合五月 | 无码国内精品久久人妻| 国产亚洲精品美女久久久| 久久久国产乱子伦精品作者| 久久A级毛片免费观看|