Thread: Dynamic list of Objects in External File

  1. #1
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050

    Dynamic list of Objects in External File

    My problem is that I need to find a way to be able to dynamically create a list of objects that are being declared in an outside file. In the outside.h file I need to declare the class and declare the actual object to make it global to all files. Inside of the outside.cpp file I will put all of the class's functions. Inside of main.cpp is where I will be determining the amount of objects in the list. As the program continues I may need to go back and add another object.

    To make it more clearer I have a class for my enemies, and I'm going to need to be able to store the information for each enemy inside of an object. The amount of objects is going to vary from time to time as each enemy is being created and dying. My guess is that I'm going to need to delete some of the objects and be adding new ones all the time.

    I've got somewhat of an idea on how to achieve all of this, and that's about it.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think of files as a container with the property that it can contain a combination of items, rather than just items of a single type. That means I can add to, delete from, etc. just like I can another container, if I know the correct syntax. It's a bit slower to use files than downloading into RAM per se', but for most occassions that isn't a big problem. To use multiple files you can use separate streams (or even separate threads, I suppose, although I'm not that facile).

    I would probably create a dedicated ifstream and ofstream for each file I want to use. When I want to add information to the file I would write to it using append mode or else I would read to a container in RAM, manipulate file contents as needed, and then write back to file again. If you have some way of knowing what data you are likely to use next, then keeping it in RAM may decrease the frequency with which you need to read/write to file.

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    No, that's not what I'm looking for at all. I need to declare my objects in a .h file not a .txt file or any sort of file like that. Here is an example of what I want to do but I know the code for it isn't right at all...

    Code:
    /*** outside.h ***/
    class Enemy
     {
       public:
        //my variables
        //my functions
     };
    
    //declaring the class as an extern and being able to keep it dynamic
    
    /*** outside.cpp ***/
    
    constructor()
    
    function()
     {
       //using individual enemy info...such as being able to say something to the effect of enemy[5].x = ....
     }
    
    /*** main.cpp ***/
    
    int main()
     {
       enemy[5].function();
     }
    I know that using arrays isn't going to allow me to add and delete objects, though. So could somebody show me how I can access and declare a list of objects in an external file that will be dynamic? Hopefully my problem is understandable enough for the rest of you.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You don't need a global list object. Just declare a list container object and use it as needed. Here's an old but reliable one, or use the STL, very user-friendly...


    Code:
    
    namespace psi {
    
    template <class type>
    class Node { public:
    type value;
    Node * next;
    Node * prev;
    Node( type data )
     :  value(data),
       prev(0),
      next(0)
     {    }
    Node( void )
     :  value(0),
       prev(0),
      next(0)
     {    }
    Node * head(){
     Node * p = this;
     while(p->prev) p = p->prev;
     return p;
    }
    Node * begin(){
     return head()->next;
    }
    Node * end(){
     Node * p = this;
      while(p->next)
       p = p->next;
     return p;
    }
    void repoint( Node * prv, Node * nxt ){
     prev = prv;
     next = nxt;
     return;
    }
    void repoint_neighbors( Node * prv, Node * nxt ){
     if(prev)
      prev->next = prv;
     if(next)
      next->prev = nxt;
     return;
    }
    Node * detach(){
     repoint_neighbors( prev, next );
     repoint(0, 0);
     return this;
    }
    Node * insert_between( Node * prv, Node * nxt ){
     repoint(prv, nxt);
     repoint_neighbors( this, this );
     return this;
    }
    Node * invisible(){ //...hide from list, NOTE: store a pointer OR ELSE!
     repoint_neighbors( prev, next );
     return this;
    }
    Node * make_visible(){ //...call from a stored pointer...
     repoint_neighbors( this, this );
     return this;
    }
    ~Node(){
     this->detach();
    }
    };
    
    template <class type>
    class List : public Node <type> { public:
    Node <type> * the_end;
    Node <type> * iter;
    List()
      : deletion_instructions(&List::place_holder){
     the_end = new_node();
     the_end->insert_between(this, 0);
    }
    ~List(){
      destroy_list();
     }
    Node <type> * end(){ //...more efficient than the Node version...
     return the_end;
    }
    Node <type> * new_node( type data = 0 ){
     return new Node<type>(data);
    }
    Node <type> * insert_front( Node <type> * node ){
     return node->insert_between( head(), begin() );
    }
    Node <type> * insert_end( Node <type> * node ){
     return node->insert_between( end()->prev, end() );
    }
    Node <type> * insert( Node <type> * node ){
     return insert_end( node );
    }
    type insert_front( type data ){
     return insert_front( new_node(data) );
    }
    type insert_end( type data ){
     insert_end( new_node(data) );
     return data;
    }
    type insert( type data ){
     return insert_end( data );
    }
    bool advance(Node <type> *& iterator){
     if(iterator == 0) return false;
     iterator = iterator->next;
     if(iterator == 0) return false;
     return true;
    }
    bool retreat(Node <type> *& iterator){
     if(iterator == 0) return false;
     iterator = iterator->prev;
     if(!iterator || iterator->prev == 0) {
      iter = 0;
      return false;
     }
     return true;
    }
    int count( Node <type> * beg, Node <type> * nd ){
     int total = 0;
     for( iter = beg; iter != nd; advance(iter) ) ++total;
     return total;
    }
    int count(){
     return count( begin(), end() );
    }
    Node <type> * find( type data, Node <type> * beg, Node <type> * nd ){
     for( iter = beg; iter != nd; advance(iter) )
      if( iter->value == data ) return iter;
     return 0;
    }
    Node <type> * find( type data ){
     return find( data, begin(), end() );
    }
    int count( type data, Node <type> * beg, Node <type> * nd ){
     int total = 0;
     for( iter = beg; iter != nd ; advance(iter) )
      if( iter->value == data ) ++total;
     return total;
    }
    int count( type data ){
     return count( data, begin(), end() );
    }
    void feed( type array[], int max, Node <type> * beg, Node <type> * nd ){
     int index = 0;
     for( iter = beg; iter != nd && index < max; advance(iter) )
      array[index++] = iter->value;
    }
    void feed( type array[], int max ){
     feed( array, max, begin(), end() );
    }
    void copy( type array[], int max, Node <type> * beg, Node <type> * nd ){
    int index = 0;
     for( iter = beg; iter != nd && index < max ; advance(iter) )
      iter->value = array[index++]; //...use what we have...
     for(  ; index < max ; advance(iter) )
      new_node(array[index++])->insert_between(end()->prev, end());       //...make some room...
    }
    void copy( type array[], int max ){
     copy(array, max, begin(), end() );
    }
    void destroy( type data ){
     destroy( find( data ) );
    }
    void destroy( Node <type> * item ){
     (*this.*deletion_instructions)( item );
    }
    void use_cell_delete(){
     deletion_instructions = &List::cell_delete;
     }
    void use_stack_delete(){
     deletion_instructions = &List::stack_delete;
     }
    void destroy_list(){
     Node <type> * node = begin();
     if( node == 0 ) {
      return;
      }
     for( iter = node->next; iter ; advance(iter) ){
      destroy(node);
      node = iter;
      }
     destroy(node);
    }
     private:
    void place_holder( Node <type> * item ){
     delete item;
    }
    void cell_delete( Node <type> * item ){
     delete item->value;
     delete item;
    }
    void stack_delete( Node <type> * item ){
     delete [] item->value;
     delete item;
    }
    void (List::*deletion_instructions)( Node <type> * item );
    };
    
    
    }// namespace
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM