473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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 :)

Jul 23 '05 #1
8 1448
* 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?
Jul 23 '05 #2
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
Jul 23 '05 #3
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
kb******@gascad.at
Jul 23 '05 #4

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 :)

Jul 23 '05 #5

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 :)

Jul 23 '05 #6

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 :)

Jul 23 '05 #7
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

Jul 23 '05 #8
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!
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Roshan Naik | last post by:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes another foo as argument and returns an int I need to achieve the above effect somehow. This is not accepted by any...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
8
by: Ronny Mandal | last post by:
Consider these two: Typedef struct { int foo, bar } FooBar; struct FooBar { int foo, bar }; What woul be the only difference here; just that I can create new instances by 'Foobar fb, *pfb',...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
17
by: Steve Carter | last post by:
Can someone please explain why the following is not possible? typedef foobar { int x; foobar *next; /* why can't we do this? */ }; Thanks
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.