Thread: elegantly interfacing a list

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    elegantly interfacing a list

    1:
    I have a list type, which looks something like this:

    Code:
    typedef struct list_s {
    	union first {
    		object obj;
    		template templ;
    	}
    };

    At the moment to add something to the list i have to use a different function according to the type.
    ie:
    void list_add_obj_item( list_t list, obj item )
    void list_add_temp_item( list_t list, templ item )

    Is there a better way to do this. Ideally i would like to be able to use the same function call. From what i've read it's not possible, but are there was to get around this, or a better way of interfacing with my list.

    Some advice from the pro's and experienced programmers would be great.

    2:
    Is it possible to redeclare a variable
    ie
    int foo;
    i want to change to
    char foo;


    Ps: sorry for asking so many stupid questions in a row, its just that i'm going through a program i have been working on for a long time and i'm trying to tidy it up a bit. (It's an RTS, I'll post on the games forum when finished )
    Last edited by Kinasz; 04-07-2004 at 09:44 AM.
    "Assumptions are the mother of all **** ups!"

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >From what i've read it's not possible
    Sure it is. You just have to think squiggly:
    Code:
    struct node {
      int type;
      union first {
        object obj;
        template templ;
      }
    };
    ...
    void list_add ( list *list, node *item );
    Now the client can write application specific makenode functions or you can provide them if you'd like:
    Code:
    list_add ( mylist, makenode ( TYPE1, new_obj ) );
    list_add ( mylist, makenode ( TYPE2, new_templ ) );
    Or, you could use a variable length argument list:
    Code:
    void list_add ( list *list, int type, ... );
    ...
    list_add ( mylist, TYPE1, obj );
    list_add ( mylist, TYPE2, templ );
    Be creative and you'll see other options as well.

    >Is it possible to redeclare a variable
    Not within the same scope. But if all you want is to change an int to a char then you have nothing to worry about as long as the int only holds char values. An int can hold all of the values that a char can and provided you use casts when necessary you'll have no problems. The opposite is not true though. You have to be careful when converting int to char because the value may not be representable by a char.
    My best code is written with the delete key.

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    cool, ok "think squigly", thanks prelude
    "Assumptions are the mother of all **** ups!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM