C/C++

Конструкторы копий С++

В интернете много приммеров с простыми интовыми переменными. А как сделать конструктор глубокого копирования если обычный конструктор так много обьектов инициализирует и создает?

Спасибо
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
template<typename T>
class Box {
public:
Box() : cap(cap), len(lim), box(new T[cap]) {}
Box(const size_t len, const T value = T()) : cap(len), len(len), box(new T[cap]) {
for (const auto& next : *this) next = value;
}
Box(initializer_list<T> lst) : cap(lst.size()), len(lst.size()), box(new T[cap]) {
copy(lst.begin(), lst.end(), this->begin());
}
Box(const Box& box) : cap(box.capacity()), len(box.size()), box(new T[cap]) {
copy(box.begin(), box.end(), this->begin());
}
~Box() {
if (box != nullptr) {
delete[] box;
box = nullptr;
}
}
Box& operator=(const Box& box) {
if (&box != this) {
delete[] this->box;
cap = box.cap;
len = box.len;
this->box = new T[cap];
copy(box.begin(), box.end(), this->begin());
}
return *this;
}
T& operator[](int index) {
return box[index];
}
void push_back(const T value) {
if (len == cap) {
auto tmp = *this;
delete[] this->box;
cap += lim;
this->box = new T[cap];
copy(tmp.begin(), tmp.end(), this->begin());
}
box[len] = value;
++len;
}
size_t capacity()const { return cap; }
size_t size()const { return len; }
size_t empty()const { return len == 0U; }
T* begin()const { return box; }
T* end()const { return box + len; }
private:
size_t cap;
size_t len;
T* box;
static const auto lim = 8U;
};
int main() {
Box a{ 1, 2, 3, 4, 5 };
for (auto x : a) cout << x << ' ';
puts("");
Box b = a;
for (auto x : b) cout << x << ' ';
puts("");
b.push_back(6);
for (auto x : b) cout << x << ' ';
puts("");
a = b;
for (auto x : b) cout << x << ' ';
puts("");
for (auto x : a) cout << x << ' ';
puts("");
b.~b();
a.push_back(7);
for (auto i = 0U; i < a.size(); ++i) cout << a[i] << ' ';
puts("");
system("pause > nul");
}
Валера Бойко
Валера Бойко
66 726
Лучший ответ
Не до конца понял что ты хочешь сделать.
Ты хочешь просто копировать? Стандартный оператор = побайтово копирует все поля одного объекта в другой. Даже перегружать его не надо
Азамат Нурланов нет так не пойдет, мне нужно сделать конструктор копирования Dip copy constructor

все обьекты созданные должны быть уникальны с уникальными адрессами
Азамат Нурланов Cave::Cave(const ::Cave &other): width(other.width), height(other.height) {

мне нужен такой конструктор, а вот я не знаю как заполнить тело конструктора