乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      請(qǐng)謹(jǐn)慎實(shí)現(xiàn)operator==操作符函數(shù)

       win2zhang 2014-10-21

      c++中,==操作符是很有用的,但是它的實(shí)現(xiàn)也并非想象中的那樣容易。本文將圍繞一個(gè)簡(jiǎn)單的c++例子程序展開(kāi)討論,以便尋求一個(gè)簡(jiǎn)單的解決方法。

      在開(kāi)始講述等于操作符之前,我們先了解一下涉及的類定義。第一個(gè)類是一個(gè)一維點(diǎn)定義,很簡(jiǎn)單。一個(gè)構(gòu)造器和析構(gòu)器,一個(gè)operator==操作符。

      Definition of Class Point1D

      class Point1D

      {

      protected:

          int xPos;

         

      public:

          Point1D( int x = 0 )

              : xPos( x )

          {

          }

       

          virtual ~Point1D()

          {

          }

         

          inline bool operator==( const Point1D &rhs )

          {

              return xPos == rhs.xPos;

          }

      };

      第二個(gè)類是Piont2D,它是一個(gè)二維點(diǎn),y軸被使用來(lái)確認(rèn)相應(yīng)位置。

      class Point2D : public Point1D

      {

      protected:

          int yPos;

         

      public:

          Point2D( int x = 0, int y = 0 )

              : Point1D( x ), yPos( y )

          {

          }

         

          virtual ~Point2D()

          {

          }

         

          inline bool operator==( const Point2D &rhs )

          {

              return (this == &rhs) || (xPos == rhs.xPos && yPos == rhs.yPos);

          }

      };

      從上面來(lái)看,兩個(gè)類好像都定義的很好,真的是這樣嗎?讓我運(yùn)行一下看看結(jié)果吧。

      #include <iostream>

      using namespace std;


      int main( void )

      {

          Point1D p1d( 10 );

          Point2D p2d( 10, 20 );

          Point2D p2d2( 10, 30 );

          Point1D *pp2d1 = &p2d;

          Point1D *pp2d2 = &p2d2;

       

          if ( p1d == p2d )

              cout << "P1D is equal to P2D" << endl;

          else

              cout << "P1D is unequal to P2D" << endl;

       

          if ( *pp2d1 == *pp2d2 )

              cout << "P2D is equal to P2D2" << endl;

          else

              cout << "P2D is unequal to P2D2" << endl;

          return 0;

      }

      Result:

      P1D is equal to P2D

      P2D is equal to P2D2

      WOW,居然P1DP2D是一樣,P2DP2D2也是一樣。顯然是一個(gè)錯(cuò)誤!錯(cuò)誤的原因在于我們錯(cuò)誤的實(shí)現(xiàn)了Point1Doperator==操作符。它沒(méi)有對(duì)它的子類進(jìn)行有效的檢查。此外,即使Point2D也重載了==操作符,也沒(méi)有使P2DP2D2能夠正確的比較。其實(shí),在多態(tài)的環(huán)境中,對(duì)于Point2D的操作符的重載往往是沒(méi)有用處的,上面就是一個(gè)很好的例子。

      我們現(xiàn)在遇到的問(wèn)題是:

      1)如何在Point1D中有效的對(duì)它的子類進(jìn)行檢查呢?

      2)如何實(shí)現(xiàn)子類中的操作符比較函數(shù)?

      對(duì)于1),我們沒(méi)有辦法判斷,因?yàn)楦割惖膶?shí)現(xiàn)代碼中是無(wú)法預(yù)測(cè)子類的。有人可能會(huì)說(shuō)為什么不把Point1D中的operator==聲明為virtual類型,然后在子類中重置呢?它的實(shí)現(xiàn)代碼可能類似于這樣:

          virtual bool operator==( const Point1D &rhs )

          {

              const Point2D *pp2d = dynamic_cast<const Point2D *>( &rhs );

              if ( pp2d == NULL )

                  return false;

              if ( this == pp2d )

                  return true;

              return xPos == pp2d->xPos && yPos == pp2d->yPos;

          }  

       

      到現(xiàn)在為止看上去好像是一個(gè)很好的主意,這樣可以在使用==的時(shí)候時(shí)候清楚的識(shí)別子父類。真的是這樣嗎?請(qǐng)看下面測(cè)試結(jié)果J.

      #include <iostream>

      using namespace std;


      int main( void )

      {

          Point1D p1d( 10 );

          Point2D p2d( 10, 20 );

          Point2D p2d2( 10, 30 );

          Point1D *pp2d1 = &p2d;

          Point1D *pp2d2 = &p2d2;

       

          if ( p1d == p2d )

              cout << "P1D is equal to P2D" << endl;

          else

              cout << "P1D is unequal to P2D" << endl;

       

          if ( p2d == p1d )

              cout << "P2D is equal to P1D" << endl;

          else

              cout << "P2D is unequal to P1D" << endl;

       

          if ( *pp2d1 == *pp2d2 )

              cout << "P2D is equal to P2D2" << endl;

          else

              cout << "P2D is unequal to P2D2" << endl;

         

          return 0;

      }

      Result:

      P1D is equal to P2D

      P2D is unequal to P1D

      P2D is unequal to P2D2

      對(duì)于Point2D之間的比較我們很好的解決了,但是對(duì)于P1DP2D之間的比較居然出現(xiàn)了戲劇性的自相矛盾!仔細(xì)研究代碼后,我們發(fā)現(xiàn)問(wèn)題還是出在Point1D==操作符上,我們依然沒(méi)有能夠合適的識(shí)別子父類。如何解決呢?對(duì)于A=B成立的話,我們一定可以獲得B=A也成立,所以我們?cè)谧?span lang="EN-US">Point1D==操作符的時(shí)候,一定要做兩次判斷等于判斷,即A == B && B == A. 請(qǐng)看下列代碼.

          virtual bool operator==( const Point1D &rhs )

          {

              return xPos == rhs.xPos && *const_cast<Point1D *>( &rhs ) == *this;

          }

      Ok,讓我們?cè)俅芜\(yùn)行測(cè)試代碼把。

      Result:

      P1D is unequal to P2D

      P2D is unequal to P1D

      P2D is unequal to P2D2

      非常好,好象我們把問(wèn)題解決了,是嗎?Point1D的問(wèn)題解決了,但是Point2D呢?它也存在這樣的問(wèn)題,我的天,這樣蹩腳的代碼還要在Point2D中重寫(xiě)一次!我簡(jiǎn)直要發(fā)瘋了!難道日后所有的子類都要這樣嗎??。?!有沒(méi)有解決方法?不要太著急,我們可以很簡(jiǎn)單的處理它。請(qǐng)看下面完整代碼.

      #include <iostream>

      using namespace std;


      class Point1D

      {

      protected:

          int xPos;

       

          virtual bool equalTo( const Point1D &rhs ) const

          {

              return xPos == rhs.xPos;

          }

         

      public:

          Point1D( int x = 0 )

              : xPos( x )

          {

          }

       

          virtual ~Point1D()

          {

          }

         

          inline bool operator==( const Point1D &rhs )

          {

              return equalTo( rhs ) && rhs.equalTo( *this );

          }

         

      };

       

      class Point2D : public Point1D

      {

      protected:

          int yPos;

         

          virtual bool equalTo( const Point1D &rhs ) const

          {

              const Point2D *pp2d = dynamic_cast<const Point2D *>( &rhs );

              if ( pp2d == NULL )

                  return false;

              if ( this == pp2d )

                  return true;

              return yPos == pp2d->yPos && Point1D::equalTo( rhs );

          }  

       

      public:

          Point2D( int x = 0, int y = 0 )

              : Point1D( x ), yPos( y )

          {

          }

         

          virtual ~Point2D()

          {

          }  

      };

       

       

      int main( void )

      {

          Point1D p1d( 10 );

          Point2D p2d( 10, 20 );

          Point2D p2d2( 10, 30 );

          Point1D *pp2d1 = &p2d;

          Point1D *pp2d2 = &p2d2;

       

          if ( p1d == p2d )

              cout << "P1D is equal to P2D" << endl;

          else

              cout << "P1D is unequal to P2D" << endl;

       

          if ( p2d == p1d )

              cout << "P2D is equal to P1D" << endl;

          else

              cout << "P2D is unequal to P1D" << endl;

       

          if ( *pp2d1 == *pp2d2 )

              cout << "P2D is equal to P2D2" << endl;

          else

              cout << "P2D is unequal to P2D2" << endl;

         

          return 0;

      }

      Result:

      P1D is unequal to P2D

      P2D is unequal to P1D

      P2D is unequal to P2D2

      我們使用了equalTo函數(shù)來(lái)完成了A==B&&B==A的雙向比較,在equalTo中,實(shí)現(xiàn)的方法和以前的完全一致,不關(guān)心比較的對(duì)象是否是父子關(guān)系。對(duì)于子類,我們僅僅重置equalTo方法就能夠正確地實(shí)現(xiàn)了operator==方法,相當(dāng)?shù)某錾?,不是嗎??/span>J

      這樣做的好處主要體現(xiàn)在以下幾點(diǎn):

      1)能夠在子類中正確地實(shí)現(xiàn)operator==方法,只要重置對(duì)應(yīng)的equalTo就可以了。

      2operator==沒(méi)有使用virtual修飾,WOW,長(zhǎng)出了一口氣,對(duì)于它的很多注意點(diǎn)終于可以解脫了。

      3)也無(wú)需為蹩腳的virtual operator==感到傷心了,因?yàn)楫吘鼓菢幼霾⒎鞘钦嬲饬x上的子類==操作符。Point2D中的操作符參數(shù)應(yīng)該是Point2D,而非Point1D,難道不是嗎J

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多