Path: csiph.com!4.us.feeder.erje.net!feeder.erje.net!2.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Tricky! Date: Tue, 30 Jul 2019 07:31:59 -0700 Organization: A noiseless patient Spider Lines: 66 Message-ID: <86mugv8ug0.fsf@mailhub.linuxsc.com> References: <7995a414-14ec-47ae-8afc-9ebcc520e30d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="de0953fb1719bfdb1d66698494fc456b"; logging-data="14919"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18l41hyXb7e1ef2yGl80kbjP7X/zeVfypc=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:pmONls71xS56sXAIWN6oKQgB6kc= sha1:1zShb26HlOvfj7/xR+0OQHltL8A= Xref: csiph.com comp.lang.c++:67203 Tiib writes: > On Monday, 29 July 2019 14:49:58 UTC+3, Bonita Montero wrote: > >> I need a class-function / method that returns a function-pointer >> to the same type of class-function. How do I implement this? I >> need this to have a kind of state-machine where each state-func- >> tion returns a pointer to the next state function. I.e I would >> write: "while( (fn = state->*fn()) != nullptr );". I see no way >> to define the type of fn since it is recursive. > > Perhaps there are no way to make a function that return function > pointer of its own type. However I do not understand the need > of it. There is state and there is its handler and if you > really need conversions between state and handler then you may > make conversion constructors/operators ... but in my humble > opinion it feels a bit messy and confusing. Here is example: > > #include > > // state > class State > { > public: > using Handler = State (*)(int); > > State(Handler h) > :handler_(h) > { } > > operator Handler() {return handler_;} > > private: > Handler handler_; > }; > > // example handlers > State DoSomethingA(int param); > > State DoSomethingB(int param); > > // example of usage > int main() > { > State::Handler s(DoSomethingA(0)); > s = s(1); > s = s(2); > s = s(3); > s = s(4); > } > > // implementations of handlers > State DoSomethingA(int param) > { > std::cout << "A: " << param << '\n'; > return DoSomethingB; > } > > State DoSomethingB(int param) > { > std::cout << "B: " << param << '\n'; > return DoSomethingA; > } Did you not understand the question? Your "solution" doesn't do anything like what he is asking for.