将Decorator对象添加到矢量 [英] Adding a Decorator object to a vector
问题描述
您好,
我有一个记录的基类,并希望使用装饰器添加其他字段,并能够链接装饰器(记录可以有电子邮件,或出生日期,或两者兼有,或无)。我也会有很多这样的装饰者;每个addtional字段一个,以及它的比较函数。完成后,我将使用基类指针将对象添加到向量。
这是代码的精确度:
Hello,
I have a base class for a record, and want to add additional fields using decorators, and be able to chain the decorators (records can have an email, or a date of birth, or both, or none). I'm also going to have a lot of such decorators; one for each addtional field, and its comparison function. Once this is done, I'm going to add the objects to a vector, using a base class pointer.
Here's a precis of the code:
class BaseRecord
{
public:
virtual bool Compare(); // defined elsewhere
protected:
std::string m_strName;
std::string m_strAddress:
};
class EmailDecorator : public BaseRecord
{
public:
EmailDecorator(BaseRecord *pBase) : m_pBase(pBase){}
bool Compare()
{
if (!CompareEmail()) // defined elsewhere
{
return false;
}
return m_pBase->Compare();
}
protected:
std::string m_strEmail
private:
BaseRecord *m_pBase;
};
class DOBDecorator : public BaseRecord
{
public:
DOBDecorator(BaseRecord *pBase) : m_pBase(pBase){}
bool Compare()
{
if (!CompareDOB()) // defined elsewhere
{
return false;
}
return m_pBase->Compare();
}
protected:
std::string m_strDOB;
private:
BaseRecord *m_pBase;
};
这些是课程。然后我想要做以下事情:
Those are the classes. I then want to do the following:
vector<BaseRecord *> m_vecRecords;
BaseRecord *pRecord = new BaseRecord();
// below is incorrect - copies only the pointer to the vector
m_vecRecords.push_back(pRecord);
// this is OK - default copy constructor used for BaseRecord
m_vecRecords.push_back(new BaseRecord(*pRecord));
// now chain the decorators
// pRecord is a BaseRecord
BaseRecord *pRecord = new EmailDecorator(pRecord);
// this is again incorrect - copies only pointer to the vector
m_vecRecords.push_back(pRecord);
// ??? what to do here - needs copy constructor for EmailDecorator
m_vecRecords.push_back(new EmailDecorator(*pRecord));
// pRecord is an EmailRecord
BaseRecord *pRecord = new DOBDecorator(pRecord);
// this is again incorrect - copies only pointer to the vector
m_vecRecords.push_back(pRecord);
// ??? what to do here - needs copy constructor for DOBDecorator
m_vecRecords.push_back(new DOBDecorator(*pRecord));
// Attempts at writing copy constructors
// should p be an EmailDecorator *p, or a BaseRecord *
EmailDecorator::EmailDecorator(const EmailDecorator *p)
{
// this wuill leak - no delete in the destructor
// in fact, there is no destructor
m_pBase = new BaseRecord(p);
m_strEmail = p->m_strEmail;
}
// should p be a DOBDecorator *, or BaseRecord *
// in the above example, when the copy constructor is needed, it is an EmailRecord *
DOBDecorator::DOBDecorator(const DOBDecorator *p)
{
// this will leak - no delete in the destructor
// in fact, there is no destructor
m_pBase = new BaseRecord(p);
m_strDOB = p->m_strDOB;
}
那么如何为装饰器编写复制构造函数?似乎必须有一种方法可以做我想要的,甚至不需要复制构造函数。
感谢您阅读本文。任何建议都将不胜感激。
Andy
So how do I write copy constructors for my decorators? It would appear that there must be a way to do what I want without even needing copy constructors.
Thanks for reading this. Any advice would be much appreciated.
Andy
推荐答案
我认为你可以使用虚拟构造函数成语如下所述: http://www.parashift.com/c++-faq-lite/virtual- ctors.html [ ^ ]。
但是我再也没有看到你提到的存储指针有什么问题(尽管事实上你不应该存储原始数据)指针,而是std :: unique_ptr或者如果需要std :: shared_ptr)
I think you can use the virtual constructor idiom as described here: http://www.parashift.com/c++-faq-lite/virtual-ctors.html[^].
But then again i dont really see whats wrong with storing the pointer as you've mentioned (despite the fact that you shouldn't store a raw pointer but rather a std::unique_ptr or if needed a std::shared_ptr )
// pRecord is a BaseRecord
BaseRecord *pRecord = new EmailDecorator(pRecord);
// this is again incorrect - copies only pointer to the vector
m_vecRecords.push_back(pRecord);
这篇关于将Decorator对象添加到矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!