I have a CRTP-class, and want to perform intermediate steps with the data passed through the interface. The goal is to be able to call arbitrary method (foo or bar) in the derived class, from B.
Edit 2: The initial example avoided some of the problems with oversimplification, but this example captures the purpose.
#include <iostream>
template <typename Derived, typename RetType>
struct A;
// Pass template arguments so that:
template <typename Base, typename Derived, typename RetType, RetType (Base::*method)(void)>
struct B
{
private:
Base *p_A_;
public:
B(Base *p_A) : p_A_(p_A) {}
RetType callAmethod()
{
return static_cast<Derived *>(p_A_)->method();
}
};
template <typename Derived, typename RetType>
struct A
{
private:
B<A<Derived, RetType>, Derived, RetType, &A<Derived, RetType>::foo> foo_B;
B<A<Derived, RetType>, Derived, RetType, &A<Derived, RetType>::bar> bar_B;
public:
RetType foo();
RetType bar();
A() : foo_B(this), bar_B(this) {}
};
template <typename Derived, typename RetType>
RetType A<Derived, RetType>::foo()
{
return foo_B.callAmethod();
}
template <typename Derived, typename RetType>
RetType A<Derived, RetType>::bar()
{
return bar_B.callAmethod();
}
struct A_Derived : public A<A_Derived, int>
{
int foo()
{
return 1;
}
int bar()
{
return 2;
}
};
int main()
{
A_Derived obj;
std::cout << obj.foo() << ", " << obj.bar() << std::endl;
}
I'm trying to 'inject' a struct in-between the methods of A and A_derived, so that A.method() -> B.method() -> A_Derived.method() Is this possible?