Welcome to ErranLi's Blog!

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

常用链接

留言簿(12)

搜索

  •  

积分与排名

  • 积分 - 169587
  • 排名 - 151

最新评论

阅读排行榜

在此聊聊我设计数据实体的一些小伎俩^_^ .........     

为了方便数据的组织.运算以及数据实体类的使用, 一般我会把所有数据实体从同一个实体基类继承而来, 基类里有一些常用的函数, 以类CEntiy为例说明如下
:

1.    CEntity(const CEntity& ent);

首先当然是拷贝构造函数, 在实际使用实体过程中这个函数很有用, 当然在CEntity的派生类中我也会去实现其派生类的拷贝构造函数
.


2.    virtual ~CEntity();

 
析构函数, 这个就不必说了,
virtual


3.  virtual CString GetTypeID(void) const;

此函数返回一个CString类型的实体标识, 以标明到底是那个类, 在基类中我一般这样实现: return  _T("ENT_UNKNOW");   很显然我会在CEntity的每一个派生类中重载这个函数, 有了这个函数, 就可以很方便的从一个基类的指针知道其实例化的到底是哪一个派生类的对象这样就可以很方便的管理系统中多个相似数据实体就可以把所有的数据实体实例化为基类的指针关于数据实体的接口都可以用基类的指针或者引用类型而不用去担心他到底是那个派生类的对象当然也可以 virtual int GetTypeID(void) const;  virtual DWORD GetTypeID(void) const; virtual DWORD GetClassID(void) const;等等。

4.        virtual void Initiate(void);

数据初始化。 初始化数据实体的数据.

 

5  virtual void Reset(void);

数据复位操作。把数据实体复位到某一状态.

 

5.        virtual CEntity * Clone(void) const;

克隆数据实体. 这个函数一般这样实现: return new CEntity(*this);

 

6.        virtual void Serialize(CArchive& ar);

数据串行化. 以实现数据实体左右移方式的对数据实体赋值,保存等操作. 常把其与CMemFile一起使用,感觉效果很好.唯一不足的是解决数据实体版本升级时的数据一致性的问题很麻烦.


7.        virtual BOOL DataSet(const CEntity& ent);

刷新数据,实现数据实体之间的赋值操作.这个函数主要是为了解决上一篇所提的问题( C++随笔关于virtual poperator = ( 05-22 01:09) ). 可以把他设为protected类型, operator=结合起来使用, poperator =调用.


8.           const CEntity& operator=(const CEntity& ent);

赋值操作.在派生类中也会重载operator=, 不是重载基类的operator=.


9.        另外如果存在数据比较的话, 会重载operator==操作符.

 friend bool operator==(const CEntity& ent1, const CEntity& ent2);

 friend bool operator!=(const CEntity& ent1, const CEntity& ent2);

 

谢谢关注~~~

以下附三相电流电压数据实体部分实现代码:

 

class CThreePhaseEntity: public CEntity

{

public:

       CThreePhaseEntity ();

       CThreePhaseEntity (const CSixPhaseEntity& ent);

       ~CSixPhaseEntity();

public:

       CString GetTypeID(void) const;

       void Initiate(void);

       void Reset(void);

 

public:

       CEntity * Clone(void) const;

       void Serialize(CArchive& ar);

       BOOL DataSet(const CEntity& ent);

 

public:

       const CSixPhaseEntity& operator=(const CSixPhaseEntity& ent);

       friend bool operator==(const CSixPhaseEntity& ent1, const CSixPhaseEntity& ent2);

       friend bool operator!=(const CSixPhaseEntity& ent1, const CSixPhaseEntity& ent2);

 

public:

// 获取三相数据 , 内联函数

       const float& GetSixPhaseData(int nDateType, int nPhaseMark) const;

       // 修改三相数据 , 内联函数

       void SetSixPhaseData(int nDataType, int nPhaseMark, const float& fData);

 

private:   

       float m_gfRange[3];  // 幅值 ,

       float m_gfPhase[3];  // 相位 ,

       float m_gfFrequency[3];  // 频率

};

 

 

 

///******cpp 文件

 

CThreePhaseEntity:: CThreePhaseEntity ()

{

       Initiate();

}

 

CThreePhaseEntity:: CThreePhaseEntity (const CThreePhaseEntity & ent)

{

       for(int i=0; i<3; i++)

       {

              m_gfRange[i] = ent.m_gfRange[i];

              m_gfPhase[i] = ent.m_gfPhase[i];

              m_gfFrequency[i] = ent.m_gfFrequency[i];

       }

}

 

CThreePhaseEntity::~ CThreePhaseEntity ()

{

}

 

CString CThreePhaseEntity::GetTypeID(void) const

{

       return _T("ENT_THREEPHASE ");

}

 

void CThreePhaseEntity::Initiate()

{

       for(int i=0; i<3; i++)

       {

              m_gfRange[i] = 0.0f;

              m_gfPhase[i] = 0.0f;

              m_gfFrequency[i] = 0.0f;

       }

}

 

void CThreePhaseEntity::Reset(void)

{

       for(int i=0; i<3; i++)

       {

              m_gfRange[i] = 57.740f;

m_gfFrequency[i] = 50.0f;

}

m_ gfPhase [0] = 0.0f

m_ gfPhase [1] = -120.0f

m_ gfPhase [2] = 120.0f

}

 

void CThreePhaseEntity::Serialize(CArchive& ar)

{

       if(ar.IsStoring())

       {

              for(int i=0; i<3; i++)

{

ar<<m_gfRange[i]<<m_gfPhase[i]<<m_gfFrequency[i];

}

       }

       else

       {

              for(int i=0; i<3; i++)

{

ar>>m_gfRange[i]>>m_gfPhase[i]>>m_gfFrequency[i];

}

       }

}

 

BOOL CThreePhaseEntity::DataSet(const CEntity& ent)

{

       if(GetTypeID() != ent.GetTypeID()) return FALSE;

      

       const CThreePhaseEntity * pEnt = reinterpret_cast<const CThreePhaseEntity *>(&ent);

 

       (*this) = (*pEnt);

 

       return TRUE;

}

 

CEntity * CThreePhaseEntity::Clone(void) const

{

       return new CThreePhaseEntity (*this);

}

 

const CThreePhaseEntity & CThreePhaseEntity::operator=(const CThreePhaseEntity & ent)

{

       if(this == &ent) return *this;

 

       for(int i=0; i<3; i++)

       {

              m_gfRange[i] = ent.m_gfRange[i];

              m_gfPhase[i] = ent.m_gfPhase[i];

              m_gfFrequency[i] = ent.m_gfFrequency[i];

       }

 

       return *this;

}

 

bool operator==(const CThreePhaseEntity & ent1, const CThreePhaseEntity & ent2)

{

       for(int i=0; i<3; i++)

       {

              if(ent1.m_gfRange[i] != ent2.m_gfRange[i]) return false;

              if(ent1.m_gfPhase[i] != ent2.m_gfPhase[i]) return false;

              if(ent1.m_gfFrequency[i] != ent2.m_gfFrequency[i]) return false;

       }

 

       return true;

}

 

bool operator!=(const CThreePhaseEntity & ent1, const CThreePhaseEntity & ent2)

{

       return (ent1 == ent2) ? false : true;

}

posted on 2006-05-30 23:14 erran 阅读(642) 评论(0)  编辑 收藏 引用 所属分类: C & C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理