mutually-recursive types and an old puzzle
Joe Buck
Joe.Buck@synopsys.COM
Wed Jun 30 19:51:00 GMT 2004
On Wed, Jun 30, 2004 at 02:59:24AM +0200, Gabriel Dos Reis wrote:
> I just gave a hint to why the fixpoint operator of C++ may suffice
> to solve the puzzle -- it wasn't an final solution.
Here's one, if anyone's curious. The class has one member, which is
a function pointer; the operator() overload calls the function pointer.
---------------------------------------------------
#include <iostream>
class FsmState {
public:
typedef class FsmState (*funcpointer)(int);
FsmState(funcpointer pfunc) : m_func(pfunc) {}
FsmState operator()(int inp) const { return (*m_func)(inp);}
private:
funcpointer m_func;
};
FsmState state_1(int inp);
FsmState state_0(int inp) {
std::cout << "0\n";
return inp ? &state_1 : &state_0;
}
FsmState state_1(int inp) {
std::cout << "1\n";
return inp ? &state_0 : &state_1;
}
int main(int,char**) {
FsmState state(&state_0);
int inp;
for (unsigned i = 0; i < 10; i++) {
std::cout << "Next input: ";
std::cin >> inp;
state = state(inp);
}
}
---------------------------------------------------
More information about the Gcc
mailing list