Typedef A references struct B which references struct A which...

Discussion in 'C++' started by DanielEKFA, May 10, 2005.

  1. DanielEKFA

    DanielEKFA Guest

    Hmm... Not sure how to crack this one. I have this code:

    typedef bool execFunctionType(const commandDataType&);

    struct commandDataType
    {
    SymbolSequence Sequence;
    string command;
    execFunctionType* executer;
    };

    As you can see, the execFunctionType takes as an argument a
    commandDataType struct which contains a pointer to a function of type
    execFunctionType. Logically this is okay (at least according to my
    logic ;), yet the thing won't compile because the struct needs the
    typedef to be defined before it to make sense of the execFunctionType,
    and the typedef needs the struct to be defined before it to make sense
    of the struct...

    What to do?

    TIA,
    Daniel :)
    DanielEKFA, May 10, 2005
    #1
    1. Advertising

  2. * DanielEKFA:
    > Hmm... Not sure how to crack this one. I have this code:
    >
    > typedef bool execFunctionType(const commandDataType&);
    >
    > struct commandDataType
    > {
    > SymbolSequence Sequence;
    > string command;
    > execFunctionType* executer;
    > };
    >
    > As you can see, the execFunctionType takes as an argument a
    > commandDataType struct which contains a pointer to a function of type
    > execFunctionType. Logically this is okay (at least according to my
    > logic ;), yet the thing won't compile because the struct needs the
    > typedef to be defined before it to make sense of the execFunctionType,
    > and the typedef needs the struct to be defined before it to make sense
    > of the struct...
    >
    > What to do?


    Best:

    use a C++ virtual member function instead of a C function pointer.

    Worst:

    struct commandDataType;

    before the typedef.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 10, 2005
    #2
    1. Advertising

  3. DanielEKFA

    Kai-Uwe Bux Guest

    DanielEKFA wrote:

    > Hmm... Not sure how to crack this one. I have this code:
    >
    > typedef bool execFunctionType(const commandDataType&);
    >
    > struct commandDataType
    > {
    > SymbolSequence Sequence;
    > string command;
    > execFunctionType* executer;
    > };
    >
    > As you can see, the execFunctionType takes as an argument a
    > commandDataType struct which contains a pointer to a function of type
    > execFunctionType. Logically this is okay (at least according to my
    > logic ;), yet the thing won't compile because the struct needs the
    > typedef to be defined before it to make sense of the execFunctionType,
    > and the typedef needs the struct to be defined before it to make sense
    > of the struct...
    >
    > What to do?


    Declare before you define:


    struct commandDataType;

    typedef bool execFunctionType(const commandDataType&);

    struct commandDataType
    {
    // other stuff
    // ...
    execFunctionType* executer;
    };


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, May 10, 2005
    #3
  4. DanielEKFA wrote:
    >
    > Hmm... Not sure how to crack this one. I have this code:
    >
    > typedef bool execFunctionType(const commandDataType&);
    >
    > struct commandDataType
    > {
    > SymbolSequence Sequence;
    > string command;
    > execFunctionType* executer;
    > };
    >
    > As you can see, the execFunctionType takes as an argument a
    > commandDataType struct which contains a pointer to a function of type
    > execFunctionType. Logically this is okay (at least according to my
    > logic ;), yet the thing won't compile because the struct needs the
    > typedef to be defined before it to make sense of the execFunctionType,
    > and the typedef needs the struct to be defined before it to make sense
    > of the struct...


    And here is your thinking flawed.
    The typedef doesn't need the struct to be completely declared. All the
    compiler needs to know is that somewhere there is a 'struct commandDataType'.
    But it doesn't need to know all the details of that struct.

    Thus a forward declaration is sufficient:

    struct commandDataType;

    typedef bool execFunctionType(const commandDataType&);

    struct commandDataType
    {
    SymbolSequence Sequence;
    string command;
    execFunctionType* executer;
    };

    Note that in C++ you rarely need function pointers. So it might be
    that you are baring up the wrong tree and what you really want is
    a class hierarchy with virtual functions.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 10, 2005
    #4
  5. DanielEKFA

    DanielEKFA Guest

    Karl Heinz Buchegger wrote:
    > DanielEKFA wrote:
    > > and the typedef needs the struct to be defined before it to make

    sense
    > > of the struct...

    >
    > And here is your thinking flawed.
    > The typedef doesn't need the struct to be completely declared. All

    the
    > compiler needs to know is that somewhere there is a 'struct

    commandDataType'.
    > But it doesn't need to know all the details of that struct.
    >


    Indeed, you and Kai are right :) Just my brain tiring down after a
    dayfull of coding, I suppose.

    > Note that in C++ you rarely need function pointers. So it might be
    > that you are baring up the wrong tree and what you really want is
    > a class hierarchy with virtual functions.
    >


    Yep, it's just that I've made a yacc/bison-like command lexer/parser
    that you teach command keywords, regular expressions to use for parsing
    the commands and their parameters, and a function pointer to use to
    execute the command in question. Also, it's being used by threads, so
    the function pointers are really useful here.

    Thanks,
    Daniel :)
    DanielEKFA, May 11, 2005
    #5
  6. DanielEKFA

    DanielEKFA Guest

    Kai-Uwe Bux wrote:
    >
    > Declare before you define:
    >
    >
    > struct commandDataType;
    >
    > typedef bool execFunctionType(const commandDataType&);
    >
    > struct commandDataType
    > {
    > // other stuff
    > // ...
    > execFunctionType* executer;
    > };
    >
    >


    Thanks, Kai-Uwe, I don't know why I didn't remember that... :) Guess
    when you learn something new, something old really does fall out ;) At
    least when you haven't used the old frequently. Anyway, I've gone an
    entirely different way, what I was trying to do wasn't really a very
    good idea, just needed to get some clarity.

    Cheers,
    Daniel :)
    DanielEKFA, May 11, 2005
    #6
  7. DanielEKFA

    DanielEKFA Guest

    Alf P. Steinbach wrote:
    > * DanielEKFA:
    > > What to do?

    >
    > Best:
    >
    > use a C++ virtual member function instead of a C function pointer.
    >


    Hi Alf, thanks for replying :) Not sure exactly how that would work
    here? Probably my code snippet is a little too select to make much
    sense :)

    > Worst:
    >
    > struct commandDataType;
    >
    > before the typedef.
    >


    Why is this bad?

    Cheers,
    Daniel :)
    DanielEKFA, May 11, 2005
    #7
  8. DanielEKFA

    Kai-Uwe Bux Guest

    DanielEKFA wrote:

    > [snip]
    > Anyway, I've gone an
    > entirely different way, what I was trying to do wasn't really a very
    > good idea, just needed to get some clarity.


    May I ask what it is that you are trying to do?


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, May 11, 2005
    #8
  9. DanielEKFA

    DanielEKFA Guest

    Kai-Uwe Bux wrote:

    > DanielEKFA wrote:
    >
    >> [snip]
    >> Anyway, I've gone an
    >> entirely different way, what I was trying to do wasn't really a very
    >> good idea, just needed to get some clarity.

    >
    > May I ask what it is that you are trying to do?
    >
    >


    Of course :) I and three group members are making an ftp-like server and
    client. For the command set, instead of writing an advanced lexer/parser
    (or write several lexers/parsers) that would need updating if we add a
    command, we've made a lexer/parser that understands a simplified regular
    expression for syntax checking, so that we can add new commands ad libitum,
    like

    Preprocessor::addExecRule("GET", "PP*P", &getFunction);

    - the preprocessor will then accept commands with the keyword GET if the
    parameters are "a path, another path, and zero to many additional paths",
    and execute the getFunction passing along the parameters as a symbol
    sequence.

    My problem before was in getting an elegant, universal implementation that
    worked on both the client and the server, as their functions execute
    differently (on the client, they execute in main, and add tasks to a global
    queue, from which threads receive their tasks, on the server, there's no
    queue, and the threads need to do the execution themselves, passing along
    their connection file descriptors). I worked around it with a typedef
    include. Not the prettiest solution, but I don't see how I can make
    templating work in a 100% static class that's never instantiated...

    Cheers,
    Daniel :)

    > Best
    >
    > Kai-Uwe Bux


    --
    Why do cats jump out of windows? Because there's love out there!
    DanielEKFA, May 16, 2005
    #9
    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. Replies:
    2
    Views:
    494
  2. S Austin

    const in typedef struct?

    S Austin, Jan 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    364
    donLouis
    Jan 2, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
  4. Steven T. Hatton
    Replies:
    2
    Views:
    663
  5. oor
    Replies:
    0
    Views:
    1,335
Loading...

Share This Page