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)?

10 Years
Discussion Span
Last Post by vijayan121

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.