Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Typedef A references struct B which references struct A which...

Reply
Thread Tools

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

 
 
DanielEKFA
Guest
Posts: n/a
 
      05-10-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-10-2005
* 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?
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-10-2005
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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-10-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
DanielEKFA
Guest
Posts: n/a
 
      05-11-2005

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

 
Reply With Quote
 
DanielEKFA
Guest
Posts: n/a
 
      05-11-2005

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

 
Reply With Quote
 
DanielEKFA
Guest
Posts: n/a
 
      05-11-2005

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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-11-2005
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

 
Reply With Quote
 
DanielEKFA
Guest
Posts: n/a
 
      05-16-2005
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
typedef struct {} SName; vs. struct SName{}; Steven T. Hatton C++ 2 08-03-2005 09:59 AM
Confusing Typedef/Struct, What Does This Do? j6vflbl6vy6g8o001@sneakemail.com C++ 2 02-17-2005 11:36 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments