如何在容器内强制执行两个独立 类 的父/派生关系?
How to enforce parent / derived relationship of two independent classes inside a container?
考虑以下 class 定义:
#include <string>
class CParty
{
public:
CParty();
virtual ~CParty();
int m_nId;
};
class CPartyEx : public CParty
{
public:
CPartyEx();
~CPartyEx();
std::string m_sName;
};
class CTransaction
{
public:
CTransaction();
virtual ~CTransaction();
int m_nClassNo;
};
class CTransactionEx : public CTransaction
{
public:
CTransactionEx();
~CTransactionEx();
std::string m_sDesc;
};
class CObject
{
public:
CObject();
~CObject();
CParty* m_pParent;
CTransaction* m_pTransaction;
};
在适当的实现中,我想创建以下类型的 CObject 存储对象:
// OK: Basic party with basic transaction
CObject iObject1;
iObject1.m_pParent = new CParty();
iObject1.m_pTransaction = new CTransaction();
// OK: Extended party with extended transaction
CObject iObject2;
iObject2.m_pParent = new CPartyEx();
iObject2.m_pTransaction = new CTransactionEx();
但是,当前的 CObject 定义并不能防止混合使用彼此不兼容的 CParty 和 CTransaction 类型:
// Not intended: Basic party with extended transaction
CObject iObject3;
iObject3.m_pParent = new CParty();
iObject3.m_pTransaction = new CTransactionEx();
// Not intended: Extended party with basic transaction
CObject iObject4;
iObject4.m_pParent = new CPartyEx();
iObject4.m_pTransaction = new CTransaction();
是否可以通过某种方式限制两个对象在 CObject 实例中的链接方式?
您可以将决定封装在 CObject
构造函数中:
class CObject
{
public:
CObject(bool extended = false);
};
CObject::CObject (bool extended)
{
if (extended)
{
m_pParent = new CPartyEx();
m_pTransaction = new CTransactionEx();
}
else
{
m_pParent = new CParty();
m_pTransaction = new CTransaction();
}
}
考虑以下 class 定义:
#include <string>
class CParty
{
public:
CParty();
virtual ~CParty();
int m_nId;
};
class CPartyEx : public CParty
{
public:
CPartyEx();
~CPartyEx();
std::string m_sName;
};
class CTransaction
{
public:
CTransaction();
virtual ~CTransaction();
int m_nClassNo;
};
class CTransactionEx : public CTransaction
{
public:
CTransactionEx();
~CTransactionEx();
std::string m_sDesc;
};
class CObject
{
public:
CObject();
~CObject();
CParty* m_pParent;
CTransaction* m_pTransaction;
};
在适当的实现中,我想创建以下类型的 CObject 存储对象:
// OK: Basic party with basic transaction
CObject iObject1;
iObject1.m_pParent = new CParty();
iObject1.m_pTransaction = new CTransaction();
// OK: Extended party with extended transaction
CObject iObject2;
iObject2.m_pParent = new CPartyEx();
iObject2.m_pTransaction = new CTransactionEx();
但是,当前的 CObject 定义并不能防止混合使用彼此不兼容的 CParty 和 CTransaction 类型:
// Not intended: Basic party with extended transaction
CObject iObject3;
iObject3.m_pParent = new CParty();
iObject3.m_pTransaction = new CTransactionEx();
// Not intended: Extended party with basic transaction
CObject iObject4;
iObject4.m_pParent = new CPartyEx();
iObject4.m_pTransaction = new CTransaction();
是否可以通过某种方式限制两个对象在 CObject 实例中的链接方式?
您可以将决定封装在 CObject
构造函数中:
class CObject
{
public:
CObject(bool extended = false);
};
CObject::CObject (bool extended)
{
if (extended)
{
m_pParent = new CPartyEx();
m_pTransaction = new CTransactionEx();
}
else
{
m_pParent = new CParty();
m_pTransaction = new CTransaction();
}
}