免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
請(qǐng)謹(jǐn)慎實(shí)現(xiàn)operator==操作符函數(shù)
在c++中,==操作符是很有用的,但是它的實(shí)現(xiàn)也并非想象中的那樣容易。本文將圍繞一個(gè)簡(jiǎn)單的c++例子程序展開(kāi)討論,以便尋求一個(gè)簡(jiǎn)單的解決方法。
在開(kāi)始講述等于操作符之前,我們先了解一下涉及的類(lèi)定義。第一個(gè)類(lèi)是一個(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è)類(lèi)是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è)類(lèi)好像都定義的很好,真的是這樣嗎?讓我運(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,居然P1D與P2D是一樣,P2D與P2D2也是一樣。顯然是一個(gè)錯(cuò)誤!錯(cuò)誤的原因在于我們錯(cuò)誤的實(shí)現(xiàn)了Point1D的operator==操作符。它沒(méi)有對(duì)它的子類(lèi)進(jìn)行有效的檢查。此外,即使Point2D也重載了==操作符,也沒(méi)有使P2D與P2D2能夠正確的比較。其實(shí),在多態(tài)的環(huán)境中,對(duì)于Point2D的操作符的重載往往是沒(méi)有用處的,上面就是一個(gè)很好的例子。
我們現(xiàn)在遇到的問(wèn)題是:
1)如何在Point1D中有效的對(duì)它的子類(lèi)進(jìn)行檢查呢?
2)如何實(shí)現(xiàn)子類(lèi)中的操作符比較函數(shù)?
對(duì)于1),我們沒(méi)有辦法判斷,因?yàn)楦割?lèi)的實(shí)現(xiàn)代碼中是無(wú)法預(yù)測(cè)子類(lèi)的。有人可能會(huì)說(shuō)為什么不把Point1D中的operator==聲明為virtual類(lèi)型,然后在子類(lèi)中重置呢?它的實(shí)現(xiàn)代碼可能類(lèi)似于這樣:
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í)別子父類(lèi)。真的是這樣嗎?請(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ì)于P1D和P2D之間的比較居然出現(xiàn)了戲劇性的自相矛盾!仔細(xì)研究代碼后,我們發(fā)現(xiàn)問(wèn)題還是出在Point1D的==操作符上,我們依然沒(méi)有能夠合適的識(shí)別子父類(lèi)。如何解決呢?對(duì)于A=B成立的話(huà),我們一定可以獲得B=A也成立,所以我們?cè)谧鯬oint1D的==操作符的時(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ā)瘋了!難道日后所有的子類(lèi)都要這樣嗎??。?!有沒(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ì)于子類(lèi),我們僅僅重置equalTo方法就能夠正確地實(shí)現(xiàn)了operator==方法,相當(dāng)?shù)某錾?,不是嗎?!J
這樣做的好處主要體現(xiàn)在以下幾點(diǎn):
1)能夠在子類(lèi)中正確地實(shí)現(xiàn)operator==方法,只要重置對(duì)應(yīng)的equalTo就可以了。
2)operator==沒(méi)有使用virtual修飾,WOW,長(zhǎng)出了一口氣,對(duì)于它的很多注意點(diǎn)終于可以解脫了。
3)也無(wú)需為蹩腳的virtual operator==感到傷心了,因?yàn)楫吘鼓菢幼霾⒎鞘钦嬲饬x上的子類(lèi)==操作符。Point2D中的操作符參數(shù)應(yīng)該是Point2D,而非Point1D,難道不是嗎J!
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
const使用詳解
C++ 引用計(jì)數(shù)技術(shù)及智能指針的簡(jiǎn)單實(shí)現(xiàn)
c++ RTTI(運(yùn)行時(shí)類(lèi)型識(shí)別)
C++中智能指針的設(shè)計(jì)和使用
構(gòu)造函數(shù)初始化和賦值的區(qū)別
第三章 class
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服