- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这样的层次结构:
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;
};
我想要的是克隆 Cube 对象和所有连接到它的对象(Sphere、SpherePair、Cube)。
Cube 内部有 Spheres,每个 Sphere 是 SpherePair 对象的一半。 SpherePair 指向位于不同立方体或同一个立方体中的球体。
这是正确撤消功能所必需的。
我还想要一张旧实体和克隆实体的 map :
std::map<Entity*, Entity*> old_new;
添加:在这些循环引用之前,我有一个简单的克隆功能:
class Entity {
public:
virtual Entity* clone() = 0;
}
它被用在这样的方案中:
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();
}
最佳答案
我将发布整个代码块作为答案:
#include <iostream>
#include <list>
#include <map>
#include <assert.h>
using std::cout;
using std::endl;
using std::list;
using std::make_pair;
using std::map;
using std::pair;
class Cube;
class Sphere;
class SpherePair;
class Entity {
public:
virtual ~Entity() {}
virtual Entity* clone() { return 0; }
virtual Entity* cloneDeep(map<Entity*, Entity*> *old_new) { return 0; }
protected:
bool cloneAndPush(Entity *e, map<Entity*, Entity*> *old_new) {
if (0 != old_new->count(e)) {
return false; // already cloned
}
typedef pair<map<Entity*, Entity*>::iterator, bool> insert_result;
Entity *cloned = e->clone();
insert_result inserted = old_new->insert(std::make_pair(e, cloned));
assert(inserted.second);
return inserted.second;
}
};
class Sphere : public Entity {
public:
Sphere() {
cout << "constructor Sphere" << endl;
}
virtual ~Sphere() {
cout << "destructor Sphere" << endl;
}
virtual Entity* clone();
virtual Entity* cloneDeep(map<Entity*, Entity*> *old_new);
Cube *cube;
SpherePair *spherepair;
};
class Cube : public Entity {
public:
Cube() {
cout << "constructor Cube" << endl;
}
virtual ~Cube() {
cout << "destructor Cube" << endl;
}
virtual Entity* clone() {
cout << "clone Cube" << endl;
Cube *c = new Cube(*this);
c->spheres_.clear();
return c;
}
virtual Entity* cloneDeep(map<Entity*, Entity*> *old_new) {
if (cloneAndPush(this, old_new)) {
Cube *c = static_cast<Cube*>((*old_new)[this]);
for(list<Sphere*>::iterator i = spheres_.begin(); i != spheres_.end(); ++i) {
c->addSphere(static_cast<Sphere*>((*i)->cloneDeep(old_new)));
}
}
return old_new->operator[](this);
}
void addSphere(Sphere *s) {
spheres_.push_back(s);
}
void delSphere(Sphere *s) {
spheres_.remove(s);
}
list<Sphere*> spheres_;
};
class SpherePair : public Entity {
public:
SpherePair() {
cout << "constructor SpherePair" << endl;
}
virtual ~SpherePair() {
cout << "destructor SpherePair" << endl;
delete first;
delete second;
}
virtual Entity* clone() {
cout << "clone SpherePair" << endl;
return new SpherePair(*this);
}
virtual Entity* cloneDeep(map<Entity*, Entity*> *old_new) {
if (cloneAndPush(this, old_new)) {
SpherePair *s = static_cast<SpherePair*>((*old_new)[this]);
s->first = (Sphere*)first->cloneDeep(old_new);
s->second = (Sphere*)second->cloneDeep(old_new);
}
return (*old_new)[this];
}
Sphere *first;
Sphere *second;
};
Entity* Sphere::clone() {
cout << "clone Sphere" << endl;
return new Sphere(*this);
}
Entity* Sphere::cloneDeep(map<Entity*, Entity*> *old_new) {
if (cloneAndPush(this, old_new)) {
Sphere *s = static_cast<Sphere*>((*old_new)[this]);
s->cube = (Cube*)cube->cloneDeep(old_new);
s->spherepair = (SpherePair*)spherepair->cloneDeep(old_new);
}
return (*old_new)[this];
}
inline void populateListSimpleCase(list<Entity*> *ents) {
Cube *first_cube = new Cube;
Cube *second_cube = new Cube;
// Cube *third_cube = new Cube;
ents->push_back(first_cube);
ents->push_back(second_cube);
for (int i = 0; i < 3; ++i) {
Sphere *first_cube_spheres = new Sphere;
Sphere *second_cube_spheres = new Sphere;
first_cube->addSphere(first_cube_spheres);
first_cube_spheres->cube = first_cube;
second_cube->addSphere(second_cube_spheres);
second_cube_spheres->cube = second_cube;
SpherePair *sp = new SpherePair;
sp->first = first_cube_spheres;
sp->second = second_cube_spheres;
ents->push_back(sp);
first_cube_spheres->spherepair = sp;
second_cube_spheres->spherepair = sp;
}
}
int main(int argc, char* argv[]) {
list<Entity*> ent_list;
populateListSimpleCase(&ent_list);
map<Entity*, Entity*> old_new;
(*ent_list.begin())->cloneDeep(&old_new);
for (list<Entity*>::iterator i = ent_list.begin(); i != ent_list.end(); ++i){
delete (*i);
}
ent_list.clear();
return 0;
}
关于c++ - 如何实现具有循环引用的对象的深拷贝或克隆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214268/
我有 jquery Draggable/droppable 来处理包含和帮助器选项集。我想做的是将放置的项目的顶部和左侧参数存储在两个变量中。 我在下面的示例中实现了这一点(将新文档图标拖到框中),但
我有一个带有两个链接下拉列表的表单,我需要制作许多克隆,但保留链接。 这是一个示例,链接组合在我的应用程序中带有 json。 链式代码:
我在使用少量 jQuery 时遇到了一些逻辑问题。 我很确定我需要一个循环设置,但我很难将其组合在一起。我引用了 tuts、视频、工作示例、幻灯片,甚至是原始 javascript,但仍然难以将逻辑端
我有一个对象,它是一个基本的树。我需要对其进行深度复制,并发现自己实现了 __clone 方法。成功的代码是: function __clone() { $object = new Custo
我可以克隆一个没有内容的文本框吗?意味着如果我在克隆后在文本框中输入一些值,我想要一个空文本框。这可能吗?或者jquery克隆将其返回为innerHtml? 最佳答案 默认情况下,克隆会复制 的值目
我想复制或克隆我自己编写的类的对象。但如果我调用复制函数,则仅复制指针。因此,如果我更改复制的对象,原始对象也会更改。 有没有一种方法/功能可以真正克隆一个对象? 最诚挚的问候梅兰妮 最佳答案 如果一
我有一些 javascripc 代码: $(this).parent().siblings().find('.year-dropdown').find('.date, .time, .details'
我们有一个包含三个命名分支的存储库,我想克隆其中一个分支。有一个善变的命令可以做到这一点吗?如果我使用 hg clone 提供(分支)路径,则会收到 404 错误。 最佳答案 hg clone htt
我有带有 ObservableCollection 和其他属性的类。所以它看起来有点像这样: public class A { public int Id { get; set; } ..
我正在尝试下载一个大型开源项目的源代码,以便我可以查看它。 它说要做: hg clone http://server/path 但是,这需要很长时间(我假设是因为这是一个大项目)。我并不真正关心变更集
我发现这段代码随处可见,用于复制列表或克隆列表。 代码随处可见: clone([],[]). clone([H|T],[H|Z]):- clone(T,Z). ?-clone([1,2,3],Z).
我正在打印一个JFrame。在此之前,我隐藏菜单栏并将 JFrame 设置为未修饰。这工作得很好,但可见的 JFrame 发生了变化,以反射(reflect)我稍后必须恢复的已删除的控件。 我想克隆
我正在尝试复制一个 div 并将其附加到它的克隆之上。不幸的是,它似乎正在创建额外的重复项。这是怎么回事? 这是一个示例:http://jsfiddle.net/QEN5N/ 最佳答案 live 会将
为什么我不能克隆 ConcurrentHashMap ? ConcurrentHashMap test = new ConcurrentHashMap(); test.put("hello",
我有这个代码: openPopup.hide(); var substr = popupId.split('-'); var clone = $("#po
这段代码几乎可以正常工作假设我的表中有 10 行,我单击顶行,它会被克隆,然后添加到表的底部,而原始数据被删除,重复这些步骤 5 次。现在,我以克隆在底部的五行结束。 现在,如果我单击第一个克隆行,它
我已经设置了JSFiddle来展示我的问题。 我改变了克隆方式,使其更加通用,因此我不需要为不同的表重用代码。通常,对于 select2 元素,我会这样做 $(".campaignType", $tr
1 2 3 $('#things').after($('#things').clone()); 克隆时如何在这两个元素之间插入中断?有没有一种巧妙的方法可以用一行代码来完成
我正在从现有类型动态装配中创建新类型,但只包含选定的属性: public class EmitTest { public Type Create(Type prototype, Type dy
在我的游戏引擎中实现对象克隆的过程中,我遇到了一些绊脚石。我的目标是拥有一个克隆系统,我不必逐个类地维护它,除非该类需要特殊处理。 我的游戏引擎的设置围绕着一个基类 Object2D,它包含一些 Te
我是一名优秀的程序员,十分优秀!