Vector of Pointers??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n355a
    New Member
    • Oct 2006
    • 4

    Vector of Pointers??

    Can anyone please give me a brief explanation as to how this vector is suppose to work. (vector<Grocery Item*> gitems; )

    Here are the classes...

    class Inventory

    {

    protected:

    vector<GroceryI tem*> gitems;

    public:

    void AddNewGrocery() ;

    void DeleteGrocery() ;

    void ModifyGrocery() ;

    void ListAllItems();

    };
    //-----------------------------

    class GroceryItem

    {

    protected:

    string itemName;

    float temPrice;

    int qtyOnHand;

    int qtyPurchased;

    public:

    virtual void PrintGrocery()= 0;

    };

    GroceryItem is a class that holds common properties of other classes such as.. class Bread, class Jam, class Cereal... all with their own special properties as well...

    Is this suppose to be a link list??? or something... I don't understand how the vector is suppose to work...

    Thanks!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The vector is not really a linked list, it is more like a variable size array, so when you want to add a grocery item you just increase the size of the vector and new another gorcery item and store a pointer to it in the new position in the vector.

    When dealing with vectors of classes (or structures) it is sometimes better to use a vector of pointers to class rather than a vector of class because if reduces the amount of copying of classes that is required (and removes the need for a copy constructor and a operator= override in respect to the vector) so the vector runs faster.

    Comment

    Working...