Answer the question
In order to leave comments, you need to log in
How to create a copy of an array of values and an array of pointers without std?
I can't figure out how to make a copy of such a structure:
struct Node {
private:
int *keys_;
int min_degree_;
Node **children_;
int count_;
bool is_leaf_;
...
Node(Node const &node) {
min_degree_ = node.min_degree_;
is_leaf_ = node.is_leaf_;
count_ = node.count_;
...
}
Answer the question
In order to leave comments, you need to log in
To copy an array, you can use memcpy
, or do it in a loop.
An array of pointers is no different from an array of values. Simply there values are pointers. Be careful not to make mistakes when using sizeof - if you pass the array itself (pointer) to it, then this will be the size of the pointer, and not the entire array. It is necessary to take the size of one element and multiply by their number.
If the original object can be deleted, then you need to redefine the move operator , not the copy operator. Internally your arrays are just pointers and can be moved around like variables:
keys_ = node.keys_;
node.keys_ = nullptr;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question