小编Arc*_*gel的帖子

将动态分配的指针数组调整为类的大小

在不使用向量的情况下如何调整数组大小?我要调整大小的数组是一个指向类的指针

class A
{
    private:
    B * b;
    int numOfItems;
    int maxNumOfItems;

    public:
    A();
    ~A();
    resizeMX();
};


A::A()
{
     numOfItems = 0;
     maxNumOfItems = 20;
     b = new B[maxNumOfItems];
     for(int i=0;i<maxNumOfItems ;i++)
     {
         b[i] = 0;
     }
}

A::~A()
{
    for(int i=0;i<numOfItems;i++)
     {
         delete [] b;
     }
}

void A::resizeMX(const B & obj)
{
     bool value=false;
     if(numOfItems<=maxNumOfItems && value == false)
     {
        //assign values to *b in for loop
     }
     else
     {
       //resize index of *b 
Run Code Online (Sandbox Code Playgroud)

我知道我们应该动态分配新的内存。像这样吗

       ++maxNumOfItems; 
        b=new B[maxNumOfItems]; …
Run Code Online (Sandbox Code Playgroud)

c++ class dynamic-memory-allocation

1
推荐指数
1
解决办法
3655
查看次数

标签 统计

c++ ×1

class ×1

dynamic-memory-allocation ×1