At 01:00 2007-02-01, John Matthews wrote:
>--- In [email protected], "Victor A. Wagner Jr." <[EMAIL PROTECTED]> 
>wrote:
> >
> > At 16:35 2007-01-31, John Matthews wrote:
> > >In some C code I've written previously, I have a home made generic
> > >list type which is told how big list items are. So list_add()
> > >allocates enough memory for a new item and returns a pointer to it, so
> > >that the caller can set the item's value. Eg. (in C, simple code to
> > >attempt to demonstrate my point)
> > >
> > >typedef struct
> > >{
> > >     int value;
> > >} Item;
> > >
> > >int main(void)
> > >{
> > >     List  items;
> > >     Item *item_p;
> > >     /* Initialise the list. */
> > >     list_init(&items, sizeof *item_p);
> > >     /* Add (uninitialised) item to end of list. */
> > >     item_p = list_add(&items);
> > >     /* Initialise the new item in the list. */
> > >     item_p->value = 3;
> > >
> > well, you're missing a fes statements....like   #include <list>
> > and   using namespace std;
> > your constructor should use an initialize also....at any rate
> > consider the following:
> >
> > #include <list>
> > using namespace std;
> > struct Item
> > {
> >     int value;
> >     Item(int v): value(v) {}
> > };
> >
> > int main()
> > {
> >     list<Item> items;
> >     items.push_back(Item(3));
> > }
> >
> > though a std::list of Item seems a bit unusual.
>
>Thanks Victor- by 'unusual', do you mean it's a bit simple? This
>example is only to demonstrate my query; if you mean something else,
>please could you explain?

I only meant that making a list of int was unusual, given the 
overhead of two pointers in each node, it gets expensive to contain 
just one int.


>In fact it's a bad example, because it doesn't show that in my real
>code it's not practical to assign the fields non-default values in the
>new item just using a constructor; I need to access the new item
>somehow either before it's added to the list or (like in the C
>example) afterwards.
>
>I'm sorry I can't post the real code yet- it's a C to C++ conversion
>which I'm working my way through, and there's too many things in it at
>the moment which you'd just laugh at (like char * strings, stdio.h etc.).
>
>John
>
>
>
>To unsubscribe, send a blank message to 
><mailto:[EMAIL PROTECTED]>.
>Yahoo! Groups Links
>
>
>

Victor A. Wagner Jr.      http://rudbek.com
The five most dangerous words in the English language:
               "There oughta be a law" 

Reply via email to