Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制int数组*_C++_Arrays - Fatal编程技术网

C++ 复制int数组*

C++ 复制int数组*,c++,arrays,C++,Arrays,我必须为我的对象创建一个副本构造函数,如下所示 class DTable { private: std::string s_name; int* *array; int size; public: DTable(); DTable(std::string sName); DTable(DTable &pcOther); ~DTable(); void vSetName(std::string sName); st

我必须为我的对象创建一个副本构造函数,如下所示

class DTable {
private:
    std::string s_name;
    int* *array;
    int size;

public:
    DTable();
    DTable(std::string sName);
    DTable(DTable &pcOther);
    ~DTable();

    void vSetName(std::string sName);
    std::string info();
    int getValue(int index, bool &ok);
    bool setValue(int index, int val);

    const int defaultArrSize = 10;
    const std::string defaultArrName = "Default Name";
};
DTable::DTable(DTable & pcOther) {
    s_name = pcOther.s_name + "_copy";
    size = pcOther.size;
    array = new int*[size];
    for (int i = 0; i < size; i++) {
        array[i] = new int;
        *array[i] = pcOther.*array[i];
    }
}
其中,数组变量指向int*数组。我想出的复制构造函数是这样的

class DTable {
private:
    std::string s_name;
    int* *array;
    int size;

public:
    DTable();
    DTable(std::string sName);
    DTable(DTable &pcOther);
    ~DTable();

    void vSetName(std::string sName);
    std::string info();
    int getValue(int index, bool &ok);
    bool setValue(int index, int val);

    const int defaultArrSize = 10;
    const std::string defaultArrName = "Default Name";
};
DTable::DTable(DTable & pcOther) {
    s_name = pcOther.s_name + "_copy";
    size = pcOther.size;
    array = new int*[size];
    for (int i = 0; i < size; i++) {
        array[i] = new int;
        *array[i] = pcOther.*array[i];
    }
}
这是错误的,因为它只是复制引用,所以在修改一个对象之后,它的副本也会被修改。我想避免这种情况

我很想使用不同的结构,但必须动态分配int*

数组。您可以使用memcpy()

例如

DTable::DTable(DTable & pcOther) {
    s_name = pcOther.s_name + "_copy";
    size = pcOther.size;
    array = new int*[size];
    for (int i = 0; i < size; i++) {
        array[i] = new int;
        *array[i] = *(pcOther.array[i]);
        // or
        memcpy ( array[i], pcOther.array[i] , sizeof(int)*1 );
    }
}
DTable::DTable(DTable&pcOther){
s_name=pcOther.s_name+“_copy”;
大小=pcOther.size;
数组=新整数*[size];
对于(int i=0;i
使用一个
std::vector
就完成了。你需要描述一下你想要表达的
array
。是的,我知道它是指向指针的指针。但是你用它干什么?二维阵列?指针数组,每个指针指向一个
int
?还是怎样我的猜测是,语句
*array[i]=pcOther.*array[i]
需要重写为
*(array[i])=*(pcOther.array[i])
,但由于不清楚您试图实现什么,这只是一个猜测。您最好使用标准容器。但是,这么说并不能帮助您理解代码中的问题。我必须为我的对象创建一个副本构造函数,-赋值运算符也可以吗?
DTable::DTable(DTable & pcOther) {
    s_name = pcOther.s_name + "_copy";
    size = pcOther.size;
    array = new int*[size];
    for (int i = 0; i < size; i++) {
        array[i] = new int;
        *array[i] = *(pcOther.array[i]);
        // or
        memcpy ( array[i], pcOther.array[i] , sizeof(int)*1 );
    }
}