Re: Conversion of pointer-to-member
Hi!
Frank Birbacher schrieb:
Well, sure there is an Instance of Base in every object of type Test.
Can I have a pointer-to-member to it?
My new approach is another level of indirection (which is known to solve
most problems ;) ). I used a new "Test" member which just points to the
object I want and then I use a pointer-to-member to just this pointer
(VARIANT 1). But that is not the way to go.
Well, the way I prefer is using a boost::function object which resolves
the object I want. I pass a Test object as an argument and get back the
Base object I want. (VARIANT 2)
#include <iostream>
#include <ostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
struct Base {
virtual void doBar() =0;
};
template<typename T>
struct Dev : Base {
Dev(T t) : t(t) {}
void doBar() { std::cout << t << '\n'; }
const T t;
};
struct Test {
Dev<int> i;
Base* const bi;
Dev<float> f;
Base* const bf;
Test()
: i(8)
, bi(&i)
, f(2.3)
, bf(&f)
{}
};
int main()
{
//VARIANT 1
Base* const Test::* const p = &Test::bi;
Test t;
(t.*p)->doBar();
//VARIANT 2, preferred
boost::function1<Base&, Test&> const p2(
boost::bind<Base&>(&Test::f,
_1) );
p2(t).doBar();
}
It just seems to me the boost::function is a bit of overkill to such a
little thing which isn't far away from regular pointer-to-members.
Frank
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]