|
|
Typedef Struct - Problems
|
|
|
|
Junior Member
Join Date: Jul 2002
Location: Australia
Status:
Offline
|
|
How will I be able to make this work??
Code:
typedef struct tagA
{
B bb;
} A;
typedef struct
{
A aa;
} B;
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status:
Offline
|
|
You can't do it, since the recursive definitions would go on forever.
I.e, if you have a variable foo of type A, you could have
foo.bb.aa.bb.aa.bb...
On the other hand you can do
Code:
typedef struct tagA
{
struct B *bb;
} A;
typedef struct
{
A *aa;
} B;
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status:
Offline
|
|
Technically, that wouldn't work either, as the second struct is not defined for the first one to know of its existence.
If you are dealing with classes, it's relatively easy. In the header files, you define one class but don't #import the other. Rather, you claim that it exists by declaring it with @class at the top of the file. You #import the header files from the implementation files. This way, you can have really complicated structures set up using pointers, but the compiler doesn't have to care.
You can do the same thing in to C++ classes using "class" instead of "@class". This also works in Objective-C++.
|
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status:
Offline
|
|
Originally posted by Detrius:
Technically, that wouldn't work either, as the second struct is not defined for the first one to know of its existence.
Sure it does. Because you're just declaring a pointer to the struct, the compiler doesn't need to know about its size. It's the same thing as in C++ when you use a forward reference to a class.
If you couldn't do this in C you'd never be able to different structures point to each other. I actually compiled the my bit of code to check the syntax.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status:
Offline
|
|
Originally posted by davechen:
Sure it does. Because you're just declaring a pointer to the struct, the compiler doesn't need to know about its size. It's the same thing as in C++ when you use a forward reference to a class.
If you couldn't do this in C you'd never be able to different structures point to each other. I actually compiled the my bit of code to check the syntax.
I've done very little with C structs. In C++ it wouldn't work without declaring the types. If you didn't declare them, then when the compiler got to the first class, it wouldn't know what the second one is and so it would spit out an error.
|
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|