Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2716704
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2008-06-24 10:30:04

#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) |
给主人留下些什么吧!~~

chinaunix网友2009-08-06 10:04:18

虽然还没有调试过,但是你给我了个重要信息,(*^__^*) 谢谢

chinaunix网友2008-06-25 10:38:58

class A { int a; public: A(int aa):a(aa){}; void funa(){ a++;}; }; class B:public A { int b; public: B(int aa,int bb):A(aa),b(bb){}; virtual void funb()=0;//pure virtual function }; class C { int c; public: C(int cc):c(cc){}; //virtual void func(){}; //注意区别 virtual void func()=0; 在C++语言中,拥有纯虚函数的类成为抽象类,该类不能实例化一个对象,必须在进一步继承中实现该函数,然后用派生类定义对象。那么,现在的问题是,基类为非抽象类,从他继承来的类是不是非抽象类呢?如果该继承类有纯虚函数,又属于抽象类还是非抽象类呢?还是先测试一下上面代码,看看结果: }; class D:public B { int d;