Hello,

I have base class and a derived class if I want to use bost::bind in an algorithm to call a virtual function that is declared in the base class I have two options:

struct Base { virtual void Go(){}};
struct Derived:Base { virtual void Go(){}};
Derived d;
// Option one
boost::bind(&Base::Go, _1) (d);
// Option two
boost::bind(&Derived::Go, _1) (d);

From my experience both options will call Derived::Go, which what I want. Is it valid to use Base::Do (it's more general, so it's easier to use the same algorithms on everything, just by changing container names)?
Thanks.

Recommended Answers

All 3 Replies

Try to do it so you'll se is it valid or not. No ideas (

I know it works in Microsoft Visual Studio 8 (Service Pack 1). But it does not mean that it suppose to work :)

yes, it is valid (and as efficient and more flexible) to use &Base::Go instead of &Derived::Go.
pointers to member functions do not support an operator(); so they are used as if boost::mem_fn has been used to convert the member pointer into a function object.
boost::bind(&Base::Go, _1) is equivalent to boost::bind( boost::mem_fn(&Base::Go), _1). internally this object stores the pointer to a member function and provides an overloaded function call operator that is implemented using the .* or ->* operator.
pointers to member functions are required to behave polymorphically when they point to virtual functions. implementations store information about the offset into the vtbl for these. and performs a vtbl lookup when they are dereferenced.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.