Thread: Node type with a choice of elements

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    Node type with a choice of elements

    So I want to create a tree with two different types of nodes, the internal nodes have pointers to further nodes, while the leaf nodes simply have elements. In Pascal, you can define a type with one element being a nodetype which can be defined to be either a leaf or an internal node. Then there is a case statement to decide what members are available for that node type. Is this possible in C++ classes or Structs, or something?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could have a base class and make internal_node and leaf derived classes. Or you can just have a node class that can be either, and use some sort of indicator to tell which one the current node is.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    I think I will go with the indicator, thank you

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    In fact the indicator is very simple -- if you're using a std::vector< Node * > (or std::deque, std::list, etc. ) as your list of child nodes, you just check to see if the vector is empty or not. Empty = leaf, non-empty = internal node.

    Note if you use pointers inside STL containers you *must* be careful to call delete on every pointer when you remove it from the vector. One option would be to wrap the pointers inside, say, a boost::shared_ptr and then put that into your vector.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Maybe a scoped_ptr. There's no need for sharing of ownership, I reckon
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Mario F.
    Maybe a scoped_ptr. There's no need for sharing of ownership, I reckon
    STL containers require that the elements they contain be copyable. Internally, STL implementations are permitted to make as many copies of an element as they choose.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ah. Gotcha!
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But a ptr_vector might be a good idea. Cheaper than the shared_ptr.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM