• <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>

            MySpace

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              18 隨筆 :: 2 文章 :: 10 評論 :: 0 Trackbacks

              當將一個派生類強制轉換成基類類型的時候,轉換方式不一樣會產生不一樣的效果。例如該代碼:static_cast<base&>(*this) ;

            base 是基類,這句代碼是將當前對象強制轉化為積累類型的對象的引用,這樣的話不會再次調用基類的構造函數。若轉換成基類對象那就要調用基類的構造函數并創建出新的對象來了。


             

            完整的代碼:

            #ifdef HAVE_CONFIG_H
            #include <config.h>
            #endif

            #include <iostream>
            #include <cstdlib>

            #include "stdio.h"
            #include <string>
            #include <iostream>

            using namespace std;


             class A

             {

             public:

              A():i_(0)

              {

               i_++;

             

               cout << " A::A() is Exce." << endl;

              }

             
             A(int i) : i_(i)
             {
              i_++;
             }


              virtual ~A()

              {

               i_--;

             

               cout << " A::~A() is Exce." << endl;

              }

             

             public:
              int i_;

             };

             

             class B : public A

             {

             public:

              B():j_(0)

              {

               j_++;

             

               cout << " B::B() is Exce." << endl;

              }

             
              B(int j) : A(j) , j_(j)
              {
               j_++;
              }
             
             


              ~B()

              {

               j_--;

             

               cout << " B::~B() is Exce." << endl;

              }
             


             public:

              int j_;

             };


            int main(int argc, char *argv[])
            {

              B *b1 = new B(5);
              B *b2 = new B(8);
             
              cout << "before changer " << endl;
             
              cout << "b1.i_ = " << b1->i_ << endl;
             
              cout << "b1.j_ = " << b1->j_ << endl;
             
              static_cast<A&>(*b1) = (*b2);
             
              cout << "affter changer " << endl;
             
              cout << "b1.i_ = " << b1->i_ << endl;
             
              cout << "b1.j_ = " << b1->j_ << endl;
             
              return 0;
             

            }

            輸出的結果是

            before changer
            b1.i_ = 6
            b1.j_ = 6
            affter changer
            b1.i_ = 9
            b1.j_ = 6

            由此可見,  static_cast<A&>(*b1) = (*b2); 類型轉換完成之后的賦值操作知識將基類的變量進行了賦值,當然,前提是基類沒有重載“=”運算符的時候,不會涉及到派生類。

             

             

            posted on 2008-06-04 15:12 yang-chunlei 閱讀(264) 評論(0)  編輯 收藏 引用
            99久久国产综合精品麻豆| 国产激情久久久久影院老熟女| 无码人妻久久一区二区三区蜜桃| 久久久精品国产Sm最大网站| 亚洲午夜精品久久久久久app| 7777久久久国产精品消防器材 | 狠狠干狠狠久久| 久久se精品一区精品二区国产| 久久精品国产99久久久香蕉| 青青草原精品99久久精品66| 国产福利电影一区二区三区久久久久成人精品综合 | 久久婷婷五月综合色高清| 国产精品一区二区久久精品无码 | 久久国产精品99国产精| 精品久久久久久国产三级| 国产A三级久久精品| 久久久WWW免费人成精品| AV无码久久久久不卡蜜桃| 亚洲国产精品综合久久网络| 久久免费精品视频| 亚洲精品乱码久久久久久久久久久久 | 无码八A片人妻少妇久久| 久久精品国产亚洲网站| 久久人人爽人人爽人人片AV不| 国产精品嫩草影院久久| 99久久成人国产精品免费| 午夜不卡久久精品无码免费| 中文字幕无码久久精品青草 | 日韩精品久久久久久久电影| 久久精品成人欧美大片| 国产精品内射久久久久欢欢| 久久精品国产亚洲欧美| 国产一级持黄大片99久久| 久久Av无码精品人妻系列| 久久久久亚洲AV无码专区体验 | 日韩一区二区久久久久久| 精品久久久久久无码专区| 国产亚洲色婷婷久久99精品| 久久偷看各类wc女厕嘘嘘| 97久久天天综合色天天综合色hd | 国产一区二区久久久|