Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #67203

Re: Tricky!

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c++
Subject Re: Tricky!
Date 2019-07-30 07:31 -0700
Organization A noiseless patient Spider
Message-ID <86mugv8ug0.fsf@mailhub.linuxsc.com> (permalink)
References <qhmmgr$dih$1@news.albasani.net> <7995a414-14ec-47ae-8afc-9ebcc520e30d@googlegroups.com>

Show all headers | View raw


Tiib <ootiib@hot.ee> 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 <iostream>
>
>     // 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.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Tricky! Bonita Montero <Bonita.Montero@gmail.com> - 2019-07-29 13:49 +0200
  Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-07-29 06:30 -0700
    Re: Tricky! "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-07-29 17:59 +0200
      Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-07-30 08:03 -0700
        Re: Tricky! "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-07-30 11:20 -0400
        Re: Tricky! "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-07-30 18:00 +0200
          Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-10 14:19 -0700
    Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-07-30 07:31 -0700
      Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-07-30 09:22 -0700
        Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-10 14:45 -0700
          Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-08-11 03:23 -0700
            Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-17 22:02 -0700
              Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-08-17 23:53 -0700
                Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-18 05:22 -0700
  Re: Tricky! Sam <sam@email-scan.com> - 2019-07-29 20:51 -0400
  Re: Tricky! Sam <sam@email-scan.com> - 2019-07-29 20:52 -0400
  Re: Tricky! Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-07-30 13:58 +0100
    Re: Tricky! Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-11 09:06 +0200

csiph-web