Creating Objects On The Heap And Adding Them To A List

This is a discussion on Creating Objects On The Heap And Adding Them To A List within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm just starting with c++. I would like to have my program create POSITION items on the fly and ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    Creating Objects On The Heap And Adding Them To A List

    Hi, I'm just starting with c++. I would like to have my program create POSITION items on the fly and add them to a CArray or CTypedPtrList or anything that will hold them. I do "POSITION* pPos = new POSITION;" but I can't get from the pointer to a new object in the list. Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    Maybe you can study something about basic data structures, like array, heap, binary tree, linked list.. I recommend you to read essays on www.aihorizon.com or maybe you can find something on this site.

    If you want to have a linked list, for example
    Code:
    class List {
    private:
      Node *root;
      Node *end;
    
    public:
      List(void) { root = end = NULL; };
      void add(int value);
    
    }
    
    void List::Add(int value)
    {
      if (root == NULL) {
        end = root = new Node;
        end->data = value;
        end->next = NULL;
        return; 
      } 
      end->next = new Node;
      end = end->next;
      end->data = value;
      end->next = NULL;  
    }
    every node should be something like
    Code:
    class Node {
    private:
      int data;
      Node *next;
    
    friend class List;
    }
    This code is not optimal, you can make it shorter and more efficient and so. Just study some materials and practise...

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    there are a number of ways to do this, depending on what you have available. One way is to use an STL list:

    Code:
    #include <list>
    #include <vector>
    .
    .
    .
    class Position
    {
      public:
        int x;
        int y;
    }
    .
    .
    .
    int main()
    {
      Position * ptr;
      list <Position *> myList;
      ptr = new Position;
      ptr->x = 1;
      ptr->y = 1;
      myList.push_back(ptr);//or myList.push_front(ptr) or myList.insert(//whatever);
    .
    .
    .
      vector <Position *> v;
      v.push_back(ptr);//or whatever;
    I suspect CArray or CTypedPtrList has something to do with VC++/MFC but since I don't use them I'm not sure about that. If your compiler is STL compliant a variation of the above should work, unless you are trying to get practice in creating your own list structure; in which case the example from mazo is a good place to start.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21