Can I write a recursive type definition ?

Discussion in 'C++' started by Timothy Madden, Sep 30, 2004.

  1. [Cross-posted to: comp.lang.c, comp.lang.c++]

    Hello all

    I'm facing a very strange problem. I need to define a function that takes as
    argument something like a pointer to itself. It takes as argument a pointer
    of the same type as a pointer to itself and with possibly other
    pointer-to-function value. But I don't have a data type for such a pointer
    because the function prototype is in the same time being defined. I hope you
    are following me ...

    Actualy I need to define a pointer to a function. Different modules of my
    app will install a new function to be pointed by this pointer, and each new
    function will remember the address of and call the previous function,
    forming a chain that exists only in code. I guess the idea is used for
    at_exit() function in the library or something like it.

    Now I want the function to take as argument a pointer to another instance of
    the same function type. This will allow for removing functions from the
    chain even in a different order than the one functions were installed, by
    having each installed function recognize it's own address in the argument
    and then return as a result the previous installed function that it knows of

    I hope you are following me...

    So how could I declare the prototype for a function that takes as argument
    and that returns a pointer to the same function type being declared ?

    Thank you
    Timothy Madden
    Romania
    --------------------------------------------------
    And I don't wanna miss a thing
     
    Timothy Madden, Sep 30, 2004
    #1
    1. Advertising

  2. Timothy Madden

    Xenos Guest

    "Timothy Madden" <> wrote in message
    news:...
    > [Cross-posted to: comp.lang.c, comp.lang.c++]
    >
    > Hello all
    >
    > I'm facing a very strange problem. I need to define a function that takes

    as
    > argument something like a pointer to itself. It takes as argument a

    pointer
    > of the same type as a pointer to itself and with possibly other
    > pointer-to-function value. But I don't have a data type for such a pointer
    > because the function prototype is in the same time being defined. I hope

    you
    > are following me ...
    >
    > Actualy I need to define a pointer to a function. Different modules of my
    > app will install a new function to be pointed by this pointer, and each

    new
    > function will remember the address of and call the previous function,
    > forming a chain that exists only in code. I guess the idea is used for
    > at_exit() function in the library or something like it.
    >
    > Now I want the function to take as argument a pointer to another instance

    of
    > the same function type. This will allow for removing functions from the
    > chain even in a different order than the one functions were installed, by
    > having each installed function recognize it's own address in the argument
    > and then return as a result the previous installed function that it knows

    of
    >
    > I hope you are following me...
    >
    > So how could I declare the prototype for a function that takes as argument
    > and that returns a pointer to the same function type being declared ?
    >
    > Thank you
    > Timothy Madden
    > Romania
    > --------------------------------------------------
    > And I don't wanna miss a thing
    >
    >


    I believe that it tells you how to handle this in the FAQ.


    Regards.
     
    Xenos, Sep 30, 2004
    #2
    1. Advertising

  3. Timothy Madden

    Guest

    "Timothy Madden" <> wrote in message news:<>...
    [snip]
    > But I don't have a data type for such a pointer
    > because the function prototype is in the same time being defined.


    You want a function that takes as argument a pointer to a function,
    which takes as argument a pointer to a function, which takes as
    argument a pointer to a function, which ...

    I'm not sure you can do it. I'm not sure you can't do it. But
    it's making me dizzy.

    I think you'd probably be better off if you defined a functional
    member of a class and then had a data member that pointed to the
    appropriate other instance of the class.
    Socks
     
    , Sep 30, 2004
    #3
  4. Timothy Madden

    Siemel Naran Guest

    "Timothy Madden" <> wrote in message

    > I'm facing a very strange problem. I need to define a function that takes

    as
    > argument something like a pointer to itself. It takes as argument a

    pointer
    > of the same type as a pointer to itself and with possibly other
    > pointer-to-function value. But I don't have a data type for such a pointer
    > because the function prototype is in the same time being defined. I hope

    you
    > are following me ...


    Could you have an array/vector/stack of function pointers or objects or
    pointers to an abstrat class object? When you want to execute the array,
    you step through each element and execute each one. So rather than
    "different modules of my app will install a new function to be pointed by
    this pointer, and each new function will remember the address of and call
    the previous function", you'll insert new function pointers or objects or
    pointers to objects into the array.

    If you literally want to call a function that returns a function, I think
    you can setup a class hierarchy and call a class member function that
    returns a new class, and this new class stores a pointer to the original
    class.

    > Actualy I need to define a pointer to a function. Different modules of my
    > app will install a new function to be pointed by this pointer, and each

    new
    > function will remember the address of and call the previous function,
    > forming a chain that exists only in code. I guess the idea is used for
    > at_exit() function in the library or something like it.
    >
    > Now I want the function to take as argument a pointer to another instance

    of
    > the same function type. This will allow for removing functions from the
    > chain even in a different order than the one functions were installed, by
    > having each installed function recognize it's own address in the argument
    > and then return as a result the previous installed function that it knows

    of
    >
    > I hope you are following me...
    >
    > So how could I declare the prototype for a function that takes as argument
    > and that returns a pointer to the same function type being declared ?
     
    Siemel Naran, Oct 1, 2004
    #4
  5. "Timothy Madden" <> wrote in message
    news:...

    > I'm facing a very strange problem. I need to define a function that takes
    > as
    > argument something like a pointer to itself. It takes as argument a
    > pointer
    > of the same type as a pointer to itself and with possibly other
    > pointer-to-function value. But I don't have a data type for such a pointer
    > because the function prototype is in the same time being defined. I hope
    > you
    > are following me ...


    You can't do it directly, but you can wrap it in a structure:

    struct Selfptr {
    Selfptr (*fp)(Selfptr);
    };
     
    Andrew Koenig, Oct 1, 2004
    #5
  6. "Andrew Koenig" <> writes:
    > "Timothy Madden" <> wrote in message
    > news:...
    >
    >> I'm facing a very strange problem. I need to define a function that takes
    >> as
    >> argument something like a pointer to itself. It takes as argument a
    >> pointer
    >> of the same type as a pointer to itself and with possibly other
    >> pointer-to-function value. But I don't have a data type for such a pointer
    >> because the function prototype is in the same time being defined. I hope
    >> you
    >> are following me ...

    >
    > You can't do it directly, but you can wrap it in a structure:
    >
    > struct Selfptr {
    > Selfptr (*fp)(Selfptr);
    > };


    Unfortunately, this thread is cross-posted to comp.lang.c and
    comp.lang.c++. In C, you need:

    struct Selfptr {
    struct Selfptr (*fp)(struct Selfptr);
    };

    (gcc gives me "warning: parameter has incomplete type".)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 1, 2004
    #6
  7. Timothy Madden

    Mabden Guest

    "Timothy Madden" <> wrote in message
    news:...
    > [Cross-posted to: comp.lang.c, comp.lang.c++]
    >
    > Hello all
    >
    > I'm facing a very strange problem. I need to define a function that

    takes as
    > argument something like a pointer to itself. It takes as argument a

    pointer
    > of the same type as a pointer to itself and with possibly other
    > pointer-to-function value. But I don't have a data type for such a

    pointer
    > because the function prototype is in the same time being defined. I

    hope you
    > are following me ...


    I think I see what you are trying to do. I once had to implement a form
    with fields that would a number of validation functions attached. If any
    one failed the chain was stopped, otherwise they would have to run down
    the list of functions.

    We implemented the validations as separate functions, and put them all
    in a validations array. The array was of type "pointer to function". Now
    all you need to is set the list of validations to the array offset. Call
    Val101, Val5, Val72, etc. Of course the names were #defined to something
    meaningful like ALL_CHARS, ZIPCODE, TELEPHONE_NUM, etc.

    In your case, you pass a (doubly) linked list of array offsets and add
    or remove items as needed. You could even restart the loop by setting
    curr_ftn to head.

    --
    Mabden
     
    Mabden, Oct 1, 2004
    #7
  8. "Andrew Koenig" <> wrote in message
    news:SUc7d.652655$...
    > "Timothy Madden" <> wrote in message
    > news:...
    >
    > > I'm facing a very strange problem. I need to define a function that

    takes
    > > as
    > > argument something like a pointer to itself. It takes as argument a
    > > pointer
    > > of the same type as a pointer to itself and with possibly other
    > > pointer-to-function value. But I don't have a data type for such a

    pointer
    > > because the function prototype is in the same time being defined. I hope
    > > you
    > > are following me ...

    >
    > You can't do it directly, but you can wrap it in a structure:
    >
    > struct Selfptr {
    > Selfptr (*fp)(Selfptr);
    > };


    I think this is the best way I can find to do it ... with some operators
    overloaded in C++
    Thank you

    Does anyone think that there should be a special class of "void code
    pointes" in C/C++ ?
    A kind of pointer that could only be assigned function addresses, without
    the pointer being of a specific function type.

    Timothy Madden
    Romania
    -----------------------------------------------
    And I don't wanna miss a thing
     
    Timothy Madden, Oct 4, 2004
    #8
  9. Timothy Madden wrote:
    > [...]
    > Does anyone think that there should be a special class of "void code
    > pointes" in C/C++ ?
    > A kind of pointer that could only be assigned function addresses, without
    > the pointer being of a specific function type.
    >


    What problem would you solve with it that cannot otherwise be solved
    with what C++ already has?

    V
     
    Victor Bazarov, Oct 4, 2004
    #9
  10. Timothy Madden

    Michael Mair Guest

    Hi Timothy,


    [Timothy Madden:]
    >>>Does anyone think that there should be a special class of "void code
    >>>pointes" in C/C++ ?
    >>>A kind of pointer that could only be assigned function addresses,
    >>>without the pointer being of a specific function type.

    >>

    [Victor Bazarov:]
    >>What problem would you solve with it that cannot otherwise be solved
    >>with what C++ already has?

    >

    [Timothy Madden:]
    > Well ... It's like 'what problem would I solve with C++ that cannot
    > otherwise be solved with what C already has ?'


    I think the question here is what kind of "problem" we are talking
    about. We could throw out some stuff from C and would still be able
    to solve the same problems.
    OTOH, C++ often speeds up development. And I think that it is here
    that there is no bottleneck and no obvious gap which this addition
    would help do away with. IMO, that is what Victor was addressing.


    > I think this could help making things clear in a good program


    How so? You would get yourself function pointers without type
    which would make it extremely hard to be sure that you are
    calling a nondescript function correctly. IMO, a "collection" of
    functions with the same return type and parameter list is the
    only sensible case for the use of a function pointer. It is
    clearer to provide wrapper functions for functions who do not
    exactly fit than to have to take care which function now is
    "behind" the "void *"-like function pointer.
    Even if you could somehow derive return type and parameter list,
    the code would not necessarily become clearer.


    Cheers
    Michael
     
    Michael Mair, Oct 5, 2004
    #10
  11. "Victor Bazarov" <> wrote in message
    news:5ic8d.3926$09.us.to.verio.net...
    > Timothy Madden wrote:
    > > [...]
    > > Does anyone think that there should be a special class of "void code
    > > pointes" in C/C++ ?
    > > A kind of pointer that could only be assigned function addresses,

    without
    > > the pointer being of a specific function type.
    > >

    >
    > What problem would you solve with it that cannot otherwise be solved
    > with what C++ already has?
    >


    Well ... It's like 'what problem would I solve with C++ that cannot
    otherwise be solved with what C already has ?'
    I think this could help making things clear in a good program

    Timothy Madden
    Romania
    -----------------------------------------------------------
    And I don't wanna miss a thing
     
    Timothy Madden, Oct 5, 2004
    #11
  12. Timothy Madden

    Tom Widmer Guest

    On Mon, 4 Oct 2004 14:53:00 +0200, "Timothy Madden"
    <> wrote:

    >
    >"Andrew Koenig" <> wrote in message
    >news:SUc7d.652655$...
    >> "Timothy Madden" <> wrote in message
    >> news:...
    >>
    >> > I'm facing a very strange problem. I need to define a function that

    >takes
    >> > as
    >> > argument something like a pointer to itself. It takes as argument a
    >> > pointer
    >> > of the same type as a pointer to itself and with possibly other
    >> > pointer-to-function value. But I don't have a data type for such a

    >pointer
    >> > because the function prototype is in the same time being defined. I hope
    >> > you
    >> > are following me ...

    >>
    >> You can't do it directly, but you can wrap it in a structure:
    >>
    >> struct Selfptr {
    >> Selfptr (*fp)(Selfptr);
    >> };

    >
    >I think this is the best way I can find to do it ... with some operators
    >overloaded in C++
    >Thank you
    >
    >Does anyone think that there should be a special class of "void code
    >pointes" in C/C++ ?
    >A kind of pointer that could only be assigned function addresses, without
    >the pointer being of a specific function type.


    Well, it is legal to cast function pointer types to other types. If I
    want a generic pointer to function, I just use void(*)(void). Just
    make sure you cast the function back to its original type before
    calling it!

    Tom
     
    Tom Widmer, Oct 5, 2004
    #12
  13. Timothy Madden

    Attila Feher Guest

    Tom Widmer wrote:
    [SNIP]
    > Well, it is legal to cast function pointer types to other types.

    [SNIP]

    I guess you meant other function pointer types?

    --
    Attila aka WW
     
    Attila Feher, Oct 5, 2004
    #13
  14. Timothy Madden

    Tom Widmer Guest

    On Tue, 5 Oct 2004 14:15:55 +0300, "Attila Feher"
    <> wrote:

    >Tom Widmer wrote:
    >[SNIP]
    >> Well, it is legal to cast function pointer types to other types.

    >[SNIP]
    >
    >I guess you meant other function pointer types?


    Right.

    Tom
     
    Tom Widmer, Oct 7, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    641
    Victor Bazarov
    Mar 13, 2005
  2. Timothy Madden

    Can I write a recursive type definition ?

    Timothy Madden, Sep 30, 2004, in forum: C Programming
    Replies:
    13
    Views:
    494
    Tom Widmer
    Oct 7, 2004
  3. Replies:
    11
    Views:
    2,107
    David Harmon
    Sep 12, 2006
  4. Pierre Yves
    Replies:
    2
    Views:
    523
    Pierre Yves
    Jan 10, 2008
  5. n00m
    Replies:
    12
    Views:
    1,141
Loading...

Share This Page