Thread: Is this safe - linked list with multiple types

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pronecracker View Post
    Because they are related, they are different kinds of shapes in some sort of drawing program. And they are possibly stored in order from back to front (Z).
    I'd suggest storing the objects themselves elsewhere, and keeping a void * pointer to the object inside the node. Functions which operate on nodes would cast the void * to the appropriate type based on the value of iType.

    This is basic polymorphism, which is much easier to handle in C++ than in C.

  2. #17
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by brewbuck View Post
    This is basic polymorphism, which is much easier to handle in C++ than in C.
    I know, please don't take my signature too literally I sometimes have thoughts like that, because C seems so simple and I sometimes get confused because of all the functionality of C++.

    But, why is using a void* safer then what I'm doing now? And what about
    Code:
    union {
    Type1 *pType1;
    Type2 *pType2;
    };
    Don't call me stupid.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pronecracker View Post
    But, why is using a void* safer then what I'm doing now? And what about
    Code:
    union {
    Type1 *pType1;
    Type2 *pType2;
    };
    I'd say it's safer because it's more likely to work on some screwy compiler. The standard indicates that what you are doing SHOULD work, but I wouldn't count on it. The union thing is fine, too. I do something very similar when writing parsers that build parse trees -- each node of the tree is of type Node, with some code which identifies what kind of node it is, and the data specific to that node type is kept in a field of a union.

    If you're going to use a union, no need to use pointers. Just embed the type-specific data directly in there.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by pronecracker View Post
    But, why is using a void* safer then what I'm doing now? And what about
    Code:
    union {
    Type1 *pType1;
    Type2 *pType2;
    };
    Fine details for varying systems.
    http://www.open-std.org/jtc1/sc22/wg...24.pdf#page=59

    Implementations have existed in which pointers to different types have different representations and/or sizes.
    http://c-faq.com/null/machexamp.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM