jrx wrote:
Quote:
The problem is, that.. it doesn't work. It appears that, my_error::what
has the type
const char* (std::runtime_error::*)() const
not:
const char* (my_error::*)() const
>
Quite tricky, because std::runtime_error also derives what() from
std::exception.
>
Is there any way to give pointer to such method as an argument?
In the above I think you need to make the signature const e.g:
template <class T>
class Wrapper {
/*...*/
//###########################################
void addMethod(Result (T::*method)() const) {
//############################################
}
};
Unfortunately the error message is misleading. Try the above and the
function should be found in the base class.
To call it I think boost::function etc won't help. You could define
your own functor maybe like this:
#include <stdexcept>
#include <iostream>
template <typename Class>
struct what_functor{
Class* m_p_class;
const char* (Class::* m_pf)()const;
what_functor(Class * p_class_in, const char* (Class::*
pf_in)()const)
: m_p_class(p_class_in),m_pf(pf_in){}
const char* operator()()
{
return ( m_p_class->*(m_pf))();
}
};
class my_error : public std::runtime_error {
public:
my_error(const std::string& msg) : std::runtime_error(msg) {}
};
int main()
{
my_error e("something");
// need a class instance pointer and the member function pointer
// to init the functor
what_functor<my_errorfunc(&e,&my_error::what);
std::cout << func() <<'\n';
}
regards
Andy Little