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();
protected:
std::string m_strName;
std::string m_strAddress:
};
class EmailDecorator : public BaseRecord
{
public:
EmailDecorator(BaseRecord *pBase) : m_pBase(pBase){}
bool Compare()
{
if (!CompareEmail()) {
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()) {
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();
m_vecRecords.push_back(pRecord);
m_vecRecords.push_back(new BaseRecord(*pRecord));
BaseRecord *pRecord = new EmailDecorator(pRecord);
m_vecRecords.push_back(pRecord);
m_vecRecords.push_back(new EmailDecorator(*pRecord));
BaseRecord *pRecord = new DOBDecorator(pRecord);
m_vecRecords.push_back(pRecord);
m_vecRecords.push_back(new DOBDecorator(*pRecord));
EmailDecorator::EmailDecorator(const EmailDecorator *p)
{
m_pBase = new BaseRecord(p);
m_strEmail = p->m_strEmail;
}
DOBDecorator::DOBDecorator(const DOBDecorator *p)
{
m_pBase = new BaseRecord(p);
m_strDOB = p->m_strDOB;
}
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