I have such hierarchy:
class Sphere;
class Cube;
class SpherePair;
class Entity {};
class Cube : public Entity {
public:
list<Sphere*> spheres_;
};
class Sphere : public Entity {
public:
Cube *cube;
SpherePair *spherepair;
};
class SpherePair : public Entity {
public:
Sphere *first;
Sphere *second;
};
What I want is to make a clone of Cube object and all the objects connected to it (Sphere, SpherePair, Cube).
Cube has Spheres inside, each Sphere is a half of SpherePair object. SpherePair points to Spheres which are in separate Cubes or in one same Cube.
This is needed for proper Undo functionality.
I would also like to have a map of old and cloned entities:
std::map<Entity*, Entity*> old_new;
Added: Before these circular references I had a simple clone functionality:
class Entity {
public:
virtual Entity* clone() = 0;
}
It was used in such a scheme:
std::vector<Entity*> selected_objects_;
void move(const vec3f &offset) {
document->beginUndo();
for(int i = 0; i < selected_objects_.size(); ++i) {
Entity *cloned = selected_objects_[i]->clone();
cloned->move(offset);
selected_objects_[i]->setDeleted(true);
document->pushToUndo(selected_objects_[i]);
document->addEntity(cloned);
}
document->endUndo();
}
I will post the entire code block as an answer: