[luau] C struct interdependency problem

Ray Strode halfline at hawaii.rr.com
Sat Jun 22 22:03:00 PDT 2002


Hi Craig.

Thanks for answering.

>    As always one wonders... well what are you really trying to do... why
>not use C++ for such a structure is what I wonder.  But if it's because you
>prefer C or whichever.
>
Yes, I like C more than C++.  C++ adds some features that facilitate OO 
programming, but there are some things
I don't like about it (I could go into it if you like, but it's not 
particularly relevant, so I'm not doing it in this message).

>Also I wonder why you want the syntax syle of
>
>typedef struct Object Object... it's more common to create like this:
>
Actually my code doesn't have that.  I normally do:

typedef struct {
 ...
} Object;

(note the anonymous tag)

I just did it in my examples because I figured it would be easier to 
rearrange things if they were separate.  Adding _t
suffix is pretty common, I know.  It's all issues of style anyway, 
though.  Everyone has their own.

>ObjectError
>Object
>Error
>LinkedList/Stack (I'm assuming the stack.h file that has LinkedList means
>that Stack is a LinkedList and it's a small typo)
>
Oh, quite right, sorry...two files.  linkedlist.h and stack.h

struct LinkedLinked { Object super_class; }

and

struct Stack { LinkedList super_class; }

>The thing I'm looking at that might "not work out" is that you want Object
>to have an Error member, and Error is a "subclass" of Object.  The
>definition you show cannot compile because when you create an Object you
>declare an Object (which has an Object (which has an Object (and so on)
>member) member as a member, so it is being told to have declare an infinite
>number of Objects to declare a single object.  You need to defer this
>declaration with a pointer instead of a reference declaration so you can
>create the member at runtime dynamically.
>
I'm not following you here.  The error member of my Object /is/ a 
pointer to (a subclass of) an Error object.
So my Object is (indirectly) composed of an Object, but it's a pointer 
to an object (Just like with a linked list or
any other data structure composed of itself).

>The place to make this indirection is the ObjectError member (the way you
>have it set up).  So I have that as a pointer to ObjectError (ObjectError
>*pError).  (see below).
>
...

>object.h
>-----
>#include "ObjectError";
>
>typedef struct Object_t
>{
>    ObjectError *pError;
>    ...
>}
>
isn't that what i'm doing in my example? (I mean other than small 
stylistic differences).
One difference is i'm maintaing Object and ObjectError in the same file.

>Hope this makes sense and is helpful.  It gets involved quick, feel free to
>ask me to explain something I've said, I didn't want to go off on too many
>tangents (I already added in C++!).
>  
>
Thanks for the quick response.  I think we may have miscommunicated 
slightly, though (Partly because of my
rather large typo between the LinkedList and Stack, I'm sure).

Just to clear everything up, this is my definition for Object again.

>>struct Object {
>>   ObjectError *error;
>>   ...
>>};
>>
And here is my tree.  I suppose the confusion could have arose from me not
putting ObjectError * in the box.  That was a conscious decision, but in
hind sight I could see how it could be confusing.

Anyway, eager to here your input.  

>>+-----Object-----+<----+
>>|/////has-a://///|     |
>>|//////...///////|     |
>>|//ObjectError///|     |
>>|//////...///////|     |
>>+----------------+     |
>>        +--------------+
>>        |
>>        .
>>+---LinkedList---+<----+
>>|/////has-a://///|     |
>>|//////...///////|     |
>>+----------------+     |
>>        +--------------+
>>        |
>>        .
>>+-----Stack------+<----+
>>|/////has-a://///|     |
>>|//////...///////|     |
>>+----------------+     |
>>        +--------------+
>>        |
>>        .
>>+--ObjectError---+
>>|/////has-a://///|
>>|//////...///////|
>>+----------------+
>>    
>>
Thanks,
--Ray




More information about the LUAU mailing list