I am trying to do a deep copy of a vector of objects. I put these template classes here as an example of what I mean. In the Test Class I have in the copy constructor I have a shallow copy I believe. What I would like to know is what is the easiest way to do a deep copy of vector of object pointers like this? Do I need a for loop? I did not include a main file because I have nothing to do in it, I just would like to understand how to do the deep copy of a vector of objects in the copy constructor. What is the most elegant way to do this?
Test header
#include <iostream>
#include <vector>
#include "Test2.h"
using namespace std;
#ifndef TEST_H
#define TEST_H
class Test {
public:
vector<Test2 *> test;
Test();
Test(const Test& orig);
virtual ~Test();
private:
};
#endif /* TEST_H */
Test source
#include "Test.h"
Test::Test() {
}
Test::Test(const Test& orig) {
this->test = orig.test;
}
Test::~Test() {
}
Test 2 header
#ifndef TEST2_H
#define TEST2_H
class Test2 {
public:
Test2();
Test2(const Test2& orig);
virtual ~Test2();
private:
};
#endif /* TEST2_H */
Test2 source
#include "Test2.h"
Test2::Test2() {
}
Test2::Test2(const Test2& orig) {
}
Test2::~Test2() {
}