Using boost function with virtual methods
Hi,
I have a simple inheritance:
class A {};
class RegressionTestScenario;
typedef A CurveGen;
typedef boost::shared_ptr<CurveGen> CurveGenPtr;
typedef boost::function
On Fri, Jul 1, 2011 at 1:06 PM,
I have a simple inheritance:
class RegressionTestScenario;
typedef boost::function
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
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
(*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.
It has nothing to do with inheritance. The following code won't compile as well:
struct A
{
void f(int i);
};
boost::function
On 7/2/11 1:25 PM, Igor R wrote:
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
(*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. It has nothing to do with inheritance. The following code won't compile as well: struct A { void f(int i); };
boost::function
func = &A::f;
Note that func = $A::f does not have the same signature a &g where g is defined as void g(A*, int); It may act in an analogous manner, but the language does not make them the same. I have seen some systems were the calling convention even put the A* in a different register for the two calls. -- Richard Damon
typedef boost::function
actionFuntion; typedef boost::shared_ptr<actionFuntion> actionFuntionPtr; <...> 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?
boost::function
On Sat, Jul 2, 2011 at 1:21 PM, Igor R
typedef boost::function
actionFuntion; typedef boost::shared_ptr<actionFuntion> actionFuntionPtr; <...> 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?
boost::function
-- is a callable with 2 arguments. &CurveGenRegressionTestScenario::changeBaseDate -- is a ptr to a member function. These 2 types are incompatible. You probably should look at Boost.Bind to accomplish your task: http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#with_member_pointer...
I was going to suggest boost::bind(), but when I put the trimmed-down code above into a source file, changed it to a boost::bind() expression and tried to compile it with g++, it complained about the type incompatibility. So there are two different problems, one of which is inheritance.
participants (4)
-
Igor R
-
Nat Linden
-
przemyslaw.sliwa@uk.bnpparibas.com
-
Richard Damon