473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

typedef bool execFunctionTyp e(const commandDataType &);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* executer;
};

As you can see, the execFunctionTyp e takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionTyp e. 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 execFunctionTyp e,
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 1461
* DanielEKFA:
Hmm... Not sure how to crack this one. I have this code:

typedef bool execFunctionTyp e(const commandDataType &);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* executer;
};

As you can see, the execFunctionTyp e takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionTyp e. 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 execFunctionTyp e,
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 execFunctionTyp e(const commandDataType &);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* executer;
};

As you can see, the execFunctionTyp e takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionTyp e. 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 execFunctionTyp e,
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 execFunctionTyp e(const commandDataType &);

struct commandDataType
{
// other stuff
// ...
execFunctionTyp e* 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 execFunctionTyp e(const commandDataType &);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* executer;
};

As you can see, the execFunctionTyp e takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionTyp e. 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 execFunctionTyp e,
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 execFunctionTyp e(const commandDataType &);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* 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 execFunctionTyp e(const commandDataType &);

struct commandDataType
{
// other stuff
// ...
execFunctionTyp e* 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::a ddExecRule("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
3867
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 compiler I have tried, even though I cant spot anything in the standard that restricts it. Is there a way to achieve this typedefintion using metaprogramming ? I noticed that boost::variant does some metaprogramming tricks to allow
30
4276
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
5690
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 everything in a /*...*/ comment and appending the 'hail world' code. Is there an easier, and, let's say, more
2
3060
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 aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
16
3846
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
2361
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', and the second demands that I prefix with struct? struct FooBar fb, *pfb?
15
2574
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 ! ) what is it supposed to do ?
17
3318
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
40471
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.