Member Function Pointer Template Inheritance
My brain hurts now. But this works:
template <class T_>
class Base
{
public:
typedef void (T_::* TestFunc)();
void run_it(T_* o,TestFunc f)
{
(o->*f)();
}
};
class Child : public Base<Child>
{
public:
void test_function()
{
std::clog << "Hello Test Function Member Pointer Template Thingie From Hell!" << std::endl;
}
void Go()
{
run_it(this,&Child::test_function);
}
};
int main()
{
Child c;
c.Go();
}
It’s the keystone of an important part of what I want my C++ unit testing library to do.
I tried having Base::run_it use the implicit ‘this’ pointer instead of needing the T_ parameter, but it pukes all over the place. I may try hitting it with a bigger hammer, play some casting games and see if I can do it without rending the fabric of the space time continuum asunder.
I has teh dubm nows.
UPDATE: Why it switched fonts in the middle there I’ve no idea.
UPDATE: Once my brain recharged I realized that since I have the T_ type in the base class I CAN use the “this” pointer by explicitly casting it. Passing “this” into the base class constructor is pretty stupid.
Tags: C++, TDD, UnitTesting