简体   繁体   English

如何将对象的动态数组传递给另一个类的参数化构造函数?

[英]How do I pass a dynamic array of objects to a parameterized constructor of another class?

I don't understand raw pointers quite well and unfortunately I'm not allowed to use <vector> s, so I can't figure out how to write a parameterized constructor of a class that has an array of another class objects as its property. 我不明白原始指针相当不错,不幸的是我不能使用<vector> S,所以我无法弄清楚如何写出具有另一个类对象作为其财产的数组类的参数的构造函数。

class A{
    ...
};

class B{
    int size;
    A *arr;
    ...
    B(int, const A *); // am I declaring this right?
}

...

B::B(int size_, const A *arr_) { // this is the constructor I'm trying to write
    size = size_;
    if (arr != nullptr) delete[] arr;
    arr = new A[size];
    memcpy(arr, arr_, sizeof(A) * size);
}

How do I pass this argument without messing up the memory? 如何在不浪费内存的情况下传递此参数? The code above doesn't work properly, so I'd like to hear some tips. 上面的代码无法正常运行,因此我想听听一些提示。 I didn't manage to google the solution although it seems like my question is already answered, I apologize in that case. 尽管似乎我的问题已经回答了,但我没有设法用谷歌搜索解决方案,对此我深表歉意。

I'm not allowed to use any safe std:: stuff. 我不允许使用任何安全的std::东西。 I need to figure out how to make it work using manual memory allocation from C. Oops, I mean from C++, thanks for pointing this out. 我需要弄清楚如何使用C中的手动内存分配使其工作。糟糕,我的意思是C ++,感谢您指出这一点。

So here's the constructor that is working for me so far: 到目前为止,这是对我有用的构造函数:

B::B(int size_, const A *arr_) {
    size = size_;
    arr = new A[size_];
    for (int i = 0; i < size; i++) arr[i] = arr_[i];
}

Thanks for everyone's time! 感谢大家的时间!

In your constructor: 在您的构造函数中:

if (arr != nullptr) delete[] arr;

arr is a class member that's not initialized at this point. arr是目前尚未初始化的类成员。 It is a garbage pointer, likely to be something other than a nullptr , and this is going to attempt to delete the garbage pointer. 它是一个垃圾指针,可能不是nullptr ,而是试图delete垃圾指针。 Obviously, this is not going to go very far. 显然,这不会走太远。

Simply remove this. 只需删除它。 Your constructor sets size and arr , and that's all that it needs to do. 您的构造函数设置sizearr ,这就是它要做的全部。 There are no existing values of size and arr to worry about: you're constructing a new object. 没有现有的sizearr值需要担心:正在构造一个新对象。

memcpy(arr, arr_, sizeof(A) * size);

This will only work if A is a POD . 仅当A 是POD时才有效。 Instead, use std::copy() , which will do the right thing in all cases. 而是使用std::copy() ,这将在所有情况下都做正确的事情。 If you can't use std::copy() in addition not being allowed to use std::vector , then write a for-loop to copy this data, manually. 如果除了不能使用std::vector之外还不能使用std::copy() ,请编写一个for循环来手动复制此数据。

There is no need to check if arr is NULL in the constructor because nothing has been assigned to arr yet. 不需要检查构造函数中arr是否为NULL,因为尚未为arr分配任何内容。

memcpy is not used for copying classes because classes typically have member functions which are stored as function pointers. memcpy不用于复制类,因为类通常具有存储为函数指针的成员函数。 your memcpy statement should not be used as it will overwrite function pointers that are created in arr when the new A[size] command is executed and will not let class B execute properly. 您的memcpy语句不应使用,因为它会覆盖执行新A [size]命令时在arr中创建的函数指针,并且不会让B类正常执行。

Using a for loop to iterate through arr_ and copying only the data members of A from arr_ to arr is the way to go. 使用for循环遍历arr_并仅将A的数据成员从arr_复制到arr是可行的方法。

For example in the for loop you can do, arr[i] = arr_[i], where i is the index in the for loop. 例如,在for循环中,您可以执行arr [i] = arr_ [i],其中i是for循环中的索引。 If class A does not have an overloaded assignment operator, a shallow copy of the member variables will happen. 如果类A没有重载的赋值运算符,则将发生成员变量的浅表副本。 If a shallow copy is not acceptable, then an overloaded assignment operator should be created in class A if it does not exist. 如果不接受浅表副本,则应在A类中创建重载的赋值运算符(如果不存在)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何为 c++ 中分配的动态对象数组中的所有对象调用参数化构造函数? - How can i call the parameterized constructor for all objects in my dynamic array of objects on allocation in c++? 如何使用参数化构造函数创建对象数组 - How to create an array of objects with parameterized constructor 将char数组传递给参数化的构造函数 - Pass char array to parameterized constructor 具有 object 动态数组的参数化构造函数 - parameterized constructor with dynamic array of object 如果将对象创建为另一个 class 的数据成员,如何将值传递给参数化构造函数? - How to pass value to parameterized constructors if there objects are being created as data members of another class? 如何将数组传递给构造函数? - How do I pass an array to a constructor? 如何在另一个类中创建一个类的参数化构造函数作为数据成员? - How to create parameterized constructor of a class in another class as a data member? 在另一个类的构造函数中初始化类对象的数组 - Initialize array of class objects in constructor of another class 如何在构造函数中将对象数组作为参数传递,然后将传递的数组复制到另一个数组(仍在构造函数中) - How to pass an array of objects as an argument in a constructor, then copy the passed array to another one (still in the constructor) 如何将我写入的对象传递给另一个类的构造函数? - How can I pass an object I wrote into a constructor of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM