#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
typedef struct _NPOINT
{
int x;
int y;
} NPOINT;
class Shape {
public:
// ...
Shape::Shape():name(_T("Share")){
point.x = 10;
point.y = 100;
};
virtual Shape* clone() const = 0; // 虚拟(拷贝)构造函数
virtual void Show()
{
cout << "Shape = " << name.c_str() << endl;
}
// ...
public:
string name;
NPOINT point;
};
class Circle : public Shape {
public:
// ...
Circle::Circle(void){
name ="Circle";
//printf("Val2 = %s\n", name);
};
virtual Shape* clone() const { return new Circle(*this); }
/*
* virtual Shape* clone() const { return new Circle(); }有*this与没有的区别:
* 有则动态复制,没有则是按初始化复制
*/
virtual void Show()
{
cout << "Circle = " << name.c_str() << endl;
}
// ...
};
class Square : public Shape {
public:
// ...
Square::Square(){
name ="Square";
};
virtual Shape* clone() const { return new Square(*this); }
virtual void Show()
{
cout << "Square = " << name.c_str() << endl;
}
// ...
};
class Fred {
public:
Fred(Shape* p) : p_(p) { assert(p != NULL); } // p must not be NULL
~Fred() { delete p_; }
Fred(const Fred& f) : p_(f.p_->clone()) { }
Fred& operator= (const Fred& f)
{
if (this != &f) { // 检查自赋值
Shape* p2 = f.p_->clone(); // Create the new one FIRST...
delete p_; // ...THEN delete the old one
p_ = p2;
}
return *this;
}
void Show()
{
cout << "Fred = " << p_->name.c_str() << endl;
}
// ...
public:
Shape* p_;
};
int _tmain(int argc, _TCHAR* argv[])
{
Circle *pCircle = new Circle();
pCircle->Show();
Square *pSquare = new Square();
pSquare->Show();
Fred *pShape = new Fred( pCircle );
const Fred *pShape2 = new Fred( pCircle );
pShape->Show();
Shape *pShapeA = (Shape *)pCircle;
pShapeA->Show();
//pShapeA->p_->Show(); //error
// 由于没有与shape有继承关系,最好不要强制转化
Fred *pShapeB = (Fred *)pSquare;
const Fred *pClone( pShape );
pClone->p_->Show();
const Fred *pClone2( pShape2 );
pClone2->p_->Show();
const Fred *pClone3( (const Fred*)pSquare );
Shape *pCn1 = pCircle->clone();
pCn1->Show();
pCircle->point.x = 15;
return 0;
}
由pCircle->point.x = 15;看出,只有Shape *pCn1 = pCircle->clone();类似的动作才是真正的Clone,
如何实现Fred Clone,还要进一步测试
***************************************
class Shape {
public:
// ...
Shape::Shape():name(_T("Share")){
point.x = 10;
point.y = 100;
count = 6;
};
Shape::~Shape(){};
//virtual Shape* clone() const = 0; // 虚拟(拷贝)构造函数
virtual Shape* clone() = 0; // 虚拟(拷贝)构造函数
virtual void Show()
{
cout << "Shape = " << name.c_str() << endl;
}
// ...
public:
string name;
NPOINT point;
private:
int count;
};
class Circle : public Shape {
public:
// ...
Circle::Circle(void){
name ="Circle";
//printf("Val2 = %s\n", name);
};
//virtual Shape* clone() const { return new Circle(*this); }
virtual Shape* clone() { return new Circle(*this); }
virtual void Show()
{
cout << "Circle = " << name.c_str() << endl;
}
// ...
};
class Square : public Shape {
public:
// ...
Square::Square(){
name ="Square";
};
// virtual Shape* clone() const { return new Square(*this); }
virtual Shape* clone() { return new Square(*this); }
virtual void Show()
{
cout << "Square = " << name.c_str() << endl;
}
// ...
public:
string tmp;
};
int _tmain(int argc, _TCHAR* argv[])
{
Circle *pCircle = new Circle();
pCircle->point.x = 16;
pCircle->Show();
Circle *pCircle2 = new Circle();
Square *pSquare = new Square();
pSquare->point.y = 11;
pSquare->tmp = "master";
pSquare->Show();
Shape *pCn1 = pCircle->clone();
Shape *pSq1 = pSquare->clone();
Shape *pSq2 = pSq1->clone();
pCn1->Show();
pCircle->point.x = 15;
delete pCircle;
delete pSquare;
return 0;
}
*****************************************
自身的Clone
说明参考:
class CLargeObject
{
public:
CLargeObject(int nVal);
CLargeObject(const CLargeObject &ob);
~CLargeObject();
CLargeObject& operator = (const CLargeObject& ob);
void SetVal(int nNewVal);
int GetVal() const;
private:
struct Data
{
public:
Data(int nVal) : m_nVal(nVal), m_nReferenceCount(1) {}
private:
friend class CLargeObject;
Data* get_own_copy() // clone if necessary
{
if (m_nReferenceCount==1)
return this;
m_nReferenceCount--;
return new Data(m_nVal);
}
// control variables.
int m_nReferenceCount;
// actual data portion
int m_nVal;
};
Data *m_pData;
};
CLargeObject::CLargeObject(int nVal)
{
m_pData = new Data(nVal);
}
CLargeObject::CLargeObject(const CLargeObject &ob) // copy constructor
{
ob.m_pData->m_nReferenceCount++;
m_pData = ob.m_pData;
}
CLargeObject::~CLargeObject()
{
if (--m_pData->m_nReferenceCount == 0)
delete m_pData;
}
CLargeObject& CLargeObject::operator = (const CLargeObject& ob) // copy assignment
{
ob.m_pData->m_nReferenceCount++;
if (--m_pData->m_nReferenceCount == 0)
delete m_pData;
m_pData = ob.m_pData;
return *this;
}
void CLargeObject::SetVal(int nNewVal)
{
m_pData = m_pData->get_own_copy();
m_pData->m_nVal = nNewVal;
}
int CLargeObject::GetVal() const
{
return m_pData->m_nVal;
}
int main() {
CLargeObject* str1 = new CLargeObject(5);
CLargeObject* str2 = new CLargeObject(6);
str1->SetVal(8);
CLargeObject *obj1 = str2;
CLargeObject obj2( *str2 );
obj2.SetVal(11);
}
阅读(2153) | 评论(2) | 转发(0) |