|
Boost Users : |
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-02-15 05:38:09
Hi Sergey,
your code would not work because you are trying to bind the _address of a function_:
&CAddress::GetName to _another address of a function_. What then happens is that bind's operator()
makes the following call:
(&CAddress::GetName->*&CItem::GetAddress)();
Instead of &CAddress::GetName you must pass an instance of CItem. But you need you code to generate:
smth like this:
//function call:
(pItem->*fct_ptr)();
where the following instantiations before the call apply:
CItem* pItem =...; //valid pointer to CItem
typedef const CAddress& (CItem::*address_fct_ptr)()const; //member function type definition
address_fct_ptr fct_ptr = &CItem::GetAddress; //pointer to member function
Some small advice: please write you email name in Latin letters, so that others can read it and
know you name as well.
Hope that helps,
Ovanes Markarian
On Thu, February 15, 2007 10:35, óÅÒÇÅÊ ïÓÔÒÏ×ÓËÉÊ wrote:
> 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 mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
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