• <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 閱讀(261) 評論(0)  編輯 收藏 引用
            久久久久人妻一区二区三区vr| 国产一级持黄大片99久久| 99久久婷婷免费国产综合精品| 久久99九九国产免费看小说| 久久99国产精品久久99| 99久久这里只有精品| 国产美女久久久| 一级女性全黄久久生活片免费| 亚洲国产成人精品91久久久 | 久久狠狠一本精品综合网| 66精品综合久久久久久久| 久久久国产一区二区三区| 亚洲国产精品综合久久一线| 精品久久久久久国产三级| 久久嫩草影院免费看夜色| 最新久久免费视频| 亚洲一本综合久久| 亚洲AV无一区二区三区久久| 久久久噜噜噜久久中文字幕色伊伊 | 欧美日韩精品久久久久| 久久天天躁夜夜躁狠狠| 国产精品99久久精品| 色综合久久中文字幕综合网| 四虎亚洲国产成人久久精品| 精品伊人久久久| 久久伊人色| 激情久久久久久久久久| 色综合久久中文字幕无码| 久久天天躁狠狠躁夜夜不卡| 要久久爱在线免费观看| 国产激情久久久久影院| 亚洲精品无码久久千人斩| 久久久久亚洲AV无码专区桃色| 久久久久国产精品熟女影院| 国产精品狼人久久久久影院| 午夜精品久久影院蜜桃| 久久精品国产91久久综合麻豆自制 | 人妻无码中文久久久久专区| 2020久久精品国产免费| 亚洲精品国产字幕久久不卡 | 久久精品国产亚洲欧美|