|
Boost Users : |
From: óÅÒÇÅÊ ïÓÔÒÏ×ÓËÉÊ (sostrovskiy_at_[hidden])
Date: 2007-02-15 04:35:55
I have a problem. Here is example of my code:
class CAddress
{
public:
std::string GetName()const;
int GetSize()const;
};
class CItem
{
public:
const CAddress& GetAddress()const;
int GetItemCount()const;
int GetItemBytes()const;
};
class CReport
{
public:
virtual ReportItem(const CItem& rItem,std::ostream& out)const = 0;
};
template<class TFormula>class CReportFormula : public CReport
{
TFormula m_f;
public:
CReportFormula(TFomula f) : m_f(f){}
virtual ReportItem(const CItem& rItem,std::ostream& out)const
{
out << m_f(rItem);
}
};
template class<TFormula> CReport* make_report(TFormula f)
{
return new CReportFormula<TFormula>(f);
}
somewhere in code:
std::vector<CReport*> apReports;
apReports.push_back(make_report(boost::mem_fun(&CItem::GetItemCount)));
apReports.push_back(make_report(boost::mem_fun(&CItem::GetItemBytes)));
it's Ok and it does work.
Now I want to output to a stream some fields of CAddress class. I can get a
reference to CAddress object with the CItem::GetAddress method. So I am
writing something like it:
apReports.push_back(make_report(boost::bind(&CAddress::GetName,boost::mem_fun(&CItem::GetAddress)));
apReports.push_back(make_report(boost::bind(&CAddress::GetSize,boost::mem_fun(&CItem::GetAddress)));
these 2 lines are not compiled. Where is my error? Can I resolve this
problem with boost library?
Now I fixed these problem with that code:
template<TResult> class CReportAddress : public CReport
{
TResult (CAddress::*m_p)()const;
public:
CReportAddress( TResult (CAddress::*_p)()const) : m_p(p){}
virtual ReportItem(const CItem& rItem,std::ostream& out)const
{
const CAddress& rAddress = rItem.GetAddress();
out << (rAddress.*m_p)();
}
};
template<class TResult>CReport* make_report(TResult (CAddress::*p)()const)
{
return new CReportAddress<TResult>(p);
}
apReports.push_back(make_report(boost::mem_fun(&CAddress::GetName)));
apReports.push_back(make_report(boost::mem_fun(&CAddress::GetSize)));
but may be there is some better and more common solution?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net