像下面这样

问题描述

我编写了一个代码,其中我使用new创建对象数组。我通过另一个new关键字和该数组的前一个索引的最后一个索引扩展该数组,如下所示。它的工作正常(构造函数正在打印“ a”个乘以净指数)。但是我不知道这是否安全吗?我的主要动机是创建一个动态数组,但我不能使用malloc,因为它不会调用构造函数。所以我正在使用new。但是idont知道使用new后如何重新分配内存。如果我使用realloc,那么程序不会给出任何错误,但是realloc不会调用构造函数。

摘要:->只想在重新分配内存时调用构造函数。

I have given the image of my code

class xy
{
    unsigned x[10],y[10],counter;
public:
    xy()
    {counter=0;}
    void setxy(unsigned a,unsigned b)
    {x[counter]=a;y[counter]=b;counter++;}
    void printxy()
    {for(unsigned i=0;i<counter;i++)cout<<"="<<x[i]<<"^3+"<<y[i]<<"^3";}
};
class unitcell
{
    unsigned long long value_cube,hasharr_len;
    xy* hasharr;
    unsigned long long* r_numberindexes;
    unsigned rarraylen;
public:
    unitcell()
    {cout<<"a";}
    unsigned long long gethasharraylen()
    {return hasharr_len;}
};
int main()
{
    unitcell *a=new unitcell[3];
    unitcell *b=a+3;
    b=new unitcell[3];
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)