处理另一个类中类动态分配的对象数组

dealing with dynamically allocated array of objects of a class in another class

本文关键字:对象 数组 另一个 处理 动态分配      更新时间:2023-10-16

我希望袋类具有动态分配但不确定如何做的项目对象数组。还需要某人查看我的其余代码。我想将项目对象添加到袋子对象中,并在其形成的ADT上执行操作。

item.h

#ifndef ITEM_H
#define ITEM_H

class Item
{
char* item_name;
public:
    Item(char *name);
    void display_item(Item i);
    ~Item();
protected:
private:
};
#endif 

item.cpp

#include "Item.h"
#include<string.h>
#include<iostream>
#include<malloc.h>
using namespace std;
 Item::Item(char* name)
 {
     item_name = new char[sizeof(name)];
     strcpy(item_name,name);
 }
 void Item::display_item(Item i)
{
    cout<<i.item_name<<" ";
}
 Item::~Item()
 {
 }

BAG.H

#include "Item.h"
#ifndef BAG_H
#define BAG_H

 class Bag
{
     int no_of_items;
     int capacity;
     Item list[];
      public:
          Bag(int no_of_items);
         void add(Item i);
         void display();
          ~Bag();
        protected:
        private:
  };
  #endif

bag.cpp

#include "Bag.h"
#include "Item.h"
#include<malloc.h>
Bag::Bag(int capacity)
{
     Item list[capacity];
    no_of_items =0;
}
 void Bag::add(Item i)
{
    if(no_of_items<capacity)
    {
        list[no_of_items] = i;
        no_of_items++;
    }
   else
   {
          cout<<"bag is full";
    }

}
 void Bag:: display()
{
     for(i=0;i<no_of_items;i++)
    {
       display_item(list[i]);
    }
 }
 Bag::~Bag()
{
   //dtor
 }

我不会在您的代码上发言,如果您有问题,请在一个单独的问题中询问,以最小的代码来重现错误。这是一个答案的末尾的链接,解释了如何为您的数组动态分配内存。如果您这样做是因为您需要一个变量尺寸的数组,我会建议一个向量。虽然类型数组的对象必须在编译时定义大小,但向量没有此要求,并且您可以使用vector_pushback轻松添加新元素。但是,如果您坚持使用数组,请在此处按照说明:http://www.cplusplus.com/forum/articles/416/

尽可能始终使用stl容器。它们具有类似的界面,并有充分的文献记载。他们中的大多数都是动态的。因此,使用STL容器(std::stringstd::vector(的代码看起来像:

#include <string>
#include <vector>
#include <iostream>
class Item {
public:
    Item(const std::string& name) : item_name(name) {}
    virtual ~Item() {}
    void display_item() {
        std::cout << item_name << " ";
    }
private:
    std::string item_name;
};
class Bag {
public:
    void add(const Item& i) {
        items.push_back(i);
    }
    void display() {
        for(std::vector<Item>::iterator it = items.begin();
                it != items.end(); ++it)
            it->display_item();
    }
private:
    std::vector<Item> items;
};

您的问题是针对如何通过动态分配实现示例的,因此上面没有STL容器的代码可以作为:

#include <cstring>
#include <algorithm>
#include <iostream>
class Item {
public:
    Item() : item_name(0) {} // required for new[] expression
    Item(const char* name) : item_name(0) {
        copyName(name);
    }
    virtual ~Item() {
        delete[] item_name; // don't forget to free the allocated memory
    }
    void display_item() const {
        std::cout << item_name << " ";
    }
    Item& operator=(const Item& other) { // required for copying
        copyName(other.item_name);
        return *this;
    }
private:
    void copyName(const char* str) {
        // get the size of the new name and delete the actual name
        const size_t name_size = strlen(str);
        delete[] item_name;
        // allocate memory for new name and copy it
        item_name = new char[name_size + 1]; // extra place for ''
        strcpy(item_name, str);
    }
    char* item_name;
};
class Bag {
public:
    Bag(size_t cap = 0) :
            items(cap ? new Item[cap] : 0), capacity(cap), size(0) {}
    virtual ~Bag() {
        delete[] items;
    }
    void add(const Item& i) {
        // "resize" the actual array if there is no space for a new item
        if(size == capacity) {
            // allocate new array and copy the actual content
            capacity += 100;
            Item* temp = new Item[capacity];
            std::copy(items, items + size, temp);
            // delete the actual array and assign the newly allocated one
            delete[] items;
            items = temp;
            temp = 0;
        }
        // add the new item
        items[size] = i;
        size++;
    }
    void display() {
        for(size_t i = 0; i < size; ++i)
            items[i].display_item();
    }
private:
    Item* items;
    size_t capacity;
    size_t size;
};