特殊情况下,江湖大佬都是有替身的;替身帮真身做事,跟他很像;你对替身说的话,相当于对真身说,替身被杀死了,本尊却没有任何事。
可能有这样一种情况,我没有使用指针,但很多地方都需要用到一个对象,例如:一棵树,我想获取到子结点,并对子结点进行赋值等;但是又不想因为拷贝,在内存中形成太多的备份。
如果有很多对象,例如:A1-A20他们的成员变量,都是某一个对象X的引用,那么外部通过这一系列对象,可以在不知道X的情况,操作X;同时不用担心X会有多个拷贝。
例如下面,对江湖的一个切面
江湖带头大哥真身的类(非常典型的一个类定义)
#include <QObject>
class DaitouDageHimSelf : public QObject
{
Q_OBJECT
public:
explicit DaitouDageHimSelf(QObject *parent = 0);
DaitouDageHimSelf(const DaitouDageHimSelf& other);
DaitouDageHimSelf &operator=(const DaitouDageHimSelf &other);
QString name() const;
void setName(const QString &strName);
QString gongfu() const;
void setGongfu(const QString &strGongfu);
void addNewGongfu(const QString &strGongfu);
void speak();
signals:
public slots:
private:
QString m_strName;
QString m_strGongfu;
};
而下面是带头大哥某一种替身的类
#include "daitoudagehimself.h"
class DaitouDageStandIn : public QObject
{
Q_OBJECT
public:
DaitouDageStandIn(const DaitouDageStandIn& other);
DaitouDageStandIn& operator=(const DaitouDageStandIn& other);
~DaitouDageStandIn();
explicit DaitouDageStandIn(DaitouDageHimSelf& dage,QObject *parent = 0);
QString name();
void gongfu();
void addNewGongfu(const QString& strGongfu);
signals:
public slots:
private:
DaitouDageHimSelf& m_dageHimSelf;
};
核心就是这最后一句!!!
用户操作的是DaitouDageStandIn的对象;可以完全不用知道DaitoudageHimSelf对象的存在。
DaitouDageStandIn datouDageStandIn1(m_dageHimself);
DaitouDageStandIn datouDageStandIn2(m_dageHimself);
DaitouDageStandIn datouDageStandIn3(m_dageHimself);
//! 替身能够说出,带头大哥会什么武功;我们以为替身就是带头大哥
datouDageStandIn1.gongfu();
替身被销毁,带头大哥对象仍然存在
替身可以不断自我复制,而真身只有一份;替身也知道带头大哥会什么功夫
DaitouDageStandIn datouDageStandIn4(datouDageStandIn1);
datouDageStandIn4.gongfu();