|
Boost Users : |
Subject: Re: [Boost-users] Using boost function with virtual methods
From: Nat Linden (nat_at_[hidden])
Date: 2011-07-02 11:59:44
On Fri, Jul 1, 2011 at 1:06 PM, <przemyslaw.sliwa_at_[hidden]> wrote:
> I have a simple inheritance:
>
> class RegressionTestScenario;
>
> typedef boost::function<void (RegressionTestScenario*, const std::string &)> actionFuntion;
>
> class RegressionTestScenario
> {
> public:
> virtual ~RegressionTestScenario() {}
> };
>
> class CurveGenRegressionTestScenario : public RegressionTestScenario
> {
> public:
> void changeBaseDate(const std::string &argument);
> };
>
> and I am trying to use the following:
>
> actionFuntion funPtr;
> funPtr = &CurveGenRegressionTestScenario::changeBaseDate;
>
> but it fails. I have no idea why. Could someone give me a hint?
The inheritance is going the wrong way. An actionFuntion instance
might be called with any RegressionTestScenario*. But you can't call a
CurveGenRegressionTestScenario method with any old
RegressionTestScenario* -- only a RegressionTestScenario* that is also
a CurveGenRegressionTestScenario* would be valid. The compiler isn't
going to synthesize logic for you to
dynamic_cast<CurveGenRegressionTestScenario&>(*your_first_param)
(which throws unless your_first_param is in fact a
CurveGenRegressionTestScenario*). If you want such logic, you'll have
to code an adapter and store that.
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