• <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 閱讀(273) 評論(0)  編輯 收藏 引用
            久久久久香蕉视频| 亚洲一级Av无码毛片久久精品| 色8久久人人97超碰香蕉987| 久久Av无码精品人妻系列| 久久久久99精品成人片三人毛片| 97热久久免费频精品99| 久久亚洲国产精品123区| 久久精品极品盛宴观看| 久久精品国产半推半就| 亚洲精品午夜国产va久久| 精品国产一区二区三区久久久狼 | 久久精品国产精品亚洲人人| 香蕉久久夜色精品国产2020| 久久精品国产91久久麻豆自制| 伊人热热久久原色播放www| 国产精品成人久久久久三级午夜电影| 热99RE久久精品这里都是精品免费| 国产福利电影一区二区三区久久老子无码午夜伦不 | 精品久久久一二三区| 嫩草影院久久99| 亚洲精品乱码久久久久久蜜桃图片| 精品久久久久久无码中文野结衣 | 香蕉久久夜色精品国产尤物| 99久久久久| 九九久久99综合一区二区| 久久经典免费视频| 香蕉aa三级久久毛片| 色婷婷狠狠久久综合五月| 久久综合丁香激情久久| 国产亚洲综合久久系列| 奇米影视7777久久精品| 人妻精品久久无码专区精东影业| 久久亚洲国产最新网站| 亚洲人成无码久久电影网站| 久久久黄片| 久久久久国产日韩精品网站| 久久精品无码一区二区app| 国产精品99久久不卡| 国产精品久久永久免费| 久久综合丝袜日本网| 国产精品一区二区久久精品|