我有
struct Parent
{
int child1;
int child2;
char child3;
float child 4;
anotherStruct child5;
};
typedef struct
{
unsigned char x;
int y;
char z;
float a;
int b;
char c;
etc ..
} anotherStruct;
Parent myFirstParent;
Parent mySecondParent;
///I want to do a deep copy of myFirstParent into mySecondParent.
//does the follwowing work for that purpose??
memcpy (&mySecondParent, &myFirstParent, sizeof(myFirstParent);
Run Code Online (Sandbox Code Playgroud)
我正在调查答案,但同时由于极端时间限制我发布了这个问题.提前致谢.
包含对象指针的任何std :: container(特别是std :: queue)的复制构造函数是否调用成员的复制构造函数以允许深层复制,或者它是否对指针值执行浅复制?
例:
/*******************************************************************************
* <summary>
* Initializes a new instance of the EventHandler class.
* </summary>
*
* <param name="handler">The handler to copy.</param>
*******************************************************************************/
EventHandler::EventHandler(const EventHandler& handler) : _eventQueue(handler._eventQueue) { }
Run Code Online (Sandbox Code Playgroud)
_eventQueue声明为:std::queue<Event*> _eventQueue;其中Event是具有复制构造函数的Base类,并且具有多个具有自己的复制构造函数的派生类.
PS:我喜欢AtomineerUtils和VisualAssistX(尤其是合并时!:D)
编辑:
鉴于下面的答案,这是一个正确的方式来创建原件的副本,使原件未经修改或复制是否与原件相反(简单的修复,但仍然是一个重要的区别)?
EventHandler::EventHandler(const EventHandler& handler) {
for(size_t i = 0; i < handler._eventQueue.size(); ++i) {
this->_eventQueue.push(new Event(handler._eventQueue._Get_container().at(i)));
}
}
Run Code Online (Sandbox Code Playgroud) 这最近一直困扰着我.假设我有一个基类Base.如果我在Base之上有多个派生类,例如DerivedA和DerivedB,那么深层副本就会变得很痛苦.
OtherClass(const OtherClass & _rhs)
{
//I have a list of Base *, now I must assign a class id to each derived class to properly create a new one.
//...
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题?
我试图搜索我的答案并发现它们与C而不是C#所以想到发布它.
我的问题可能在这里微不足道.
根据我的理解(简单来说)
复制完成后
浅复制 - >主对象和复制对象(引用或值类型)应指向内存中的同一对象
DeepCopy - >主对象和复制对象(引用或值类型)应指向内存中的不同对象
继续这一点,我在C#中有一个结构,并尝试制作相同的浅层副本.我尝试使用"MemberwiseClone"方法,但我想它只适用于引用类型.对于值类型,我认为"MemberwiseClone"方法会将其装入对象并将其拆箱到堆栈中的不同内存地址.
我试过的如下.
我的问题是如何(如果可能的话)我可以创建一个简单结构的浅表副本?
我希望我的基本原理在这里是正确的,而不是说垃圾.如果我在任何陈述中出错,请纠正我.
问候,
萨马
struct MyStruct : ICloneable
{
public int MyProperty { get; set; }
public object Clone()
{
return this.MemberwiseClone();//boxing into object
}
}
private void btnChkStr_Click(object sender, EventArgs e)
{
MyStruct struct1 = new MyStruct();
struct1.MyProperty = 1;
//MyStruct struct2 = struct1; //This will create a deep copy
MyStruct struct2 = (MyStruct)(struct1.Clone());//unboxing into structure hence allocating a different memory address
struct2.MyProperty = 2;
MessageBox.Show(struct1.MyProperty.ToString()); //still …Run Code Online (Sandbox Code Playgroud) 我在我的单例类中有一个NSMutableArray调用playersArray,它为我的应用程序主数据源保存.每个对象playersArray都是a NSDictionary,内容如下:
{
sgfID = 1;
sgfPlayer = "<PlayerContact: 0xbf851b0>";
}
Run Code Online (Sandbox Code Playgroud)
PlayerContact是一个NSObject包含以下属性的子类:NSString*playerName,playerTeam,BOOL PlayerSelected等.
在我的一个ViewControllers中viewDidLoad,我想把一个深入的副本playersArray带到一个NSMutableArray命名的duplicatePlayersArray.我是这样做的
playersArray = [[SGFHelper sharedHelpers] SGFContactsArray];
duplicatePlayersArray = [playersArray mutableCopy];
Run Code Online (Sandbox Code Playgroud)
现在我有两个单独的副本,我的印象是playersArray和duplicatePlayersArray是内存中两个完全不同的数组.但是我发现它们不是!
即使调试器显示它们具有不同的内存地址,它们的内容也具有相同的内存地址.所以当我这样做时:
[((NSMutableDictionary *)[duplicatePlayersArray objectAtIndex:0]) setObject:@"333" forKey:@"sgfID"];
Run Code Online (Sandbox Code Playgroud)
playersArray的索引字典:0在上面的代码行运行之前已经使用了"333"作为关键字"sgfID"而不是"1".
但是,如果我运行下面的代码,那么两个数组开始不同
[duplicatePlayersArray replaceObjectAtIndex:0 withObject:tempDict];
Run Code Online (Sandbox Code Playgroud)
这仍然没有解决我的担忧,因为我想要相信的两个阵列仍然是"连接"的.一次更改会导致另一个数组更改其内容.
请各位朋友请给我一个深入复制数组的方法我解释了内容,其中所有内容都保存在不同的对象中.
我有这个树与不同类型的节点,我需要进行深层复制.层次结构看起来像这样:
class AllNodes
{
//this is a purely virtual base class
};
class TreeNode : public AllNodes
{
AllNodes *rChild, *lChild;
};
class LeefNode : public AllNodes
{
int value;
};
Run Code Online (Sandbox Code Playgroud)
问题是,当我想要对整个树进行深层复制时,我不知道哪些节点会有子节点以及哪些节点将具有值.我试过这个,但它不会工作(原因很明显):
void AllNodes::deepCopy(AllNodes* &copied, AllNodes* o)
{
if(o->rChild == nullptr)
copied->rChild = nullptr;
else
{
copied->rChild = o->rChild;
deepCopy(copied->rchild, o->rChild);
}
if(o->lChild == nullptr)
copied->lChild = nullptr;
else
{
copied->lChild = o->lChild;
deepCopy(copied->lChild, o->lChild);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人对如何实现这一点有一些想法?
我正在尝试为我的结构构建一个深度复制功能.在主程序中,我尝试深入复制到c,所以最后它应该打印字符串"B".我究竟做错了什么?我知道我现在缺少一些模糊的指针.
#include <iostream>
using namespace std;
struct Thing {
size_t length;
std::string txt;
struct Thing *things[];
};
struct Thing *deepCopy(struct Thing *origin) {
Thing tmp;
tmp.length = origin->length;
for(int i=0;i<tmp.length; ++i)
tmp.things[i] = deepCopy(origin->things[i]);
return &tmp;
}
int main() {
Thing a, b, *c;
a.length = 1;
a.things[0] = &b;
a.txt = "A";
b.txt = "B";
b.length = 0;
c = deepCopy(&a);
cout<<c->txt;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在努力克隆一个引用类型的列表列表.我尝试ICloneable在我的引用类中实现,但是,它似乎没有调用Clone()它中的方法.
码:
public class Solid : ICloneable{
private double[,] _points; //vertices do solido
private int[,] _edges; //arestas do solido
public int[,] Faces { get; private set; } //faces do solido
public int[,] Edges {
get { return _edges; }
set { _edges = value; }
}
...
public object Clone() {
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
ms.Position = 0;
object obj = bf.Deserialize(ms);
ms.Close();
return obj;
}
}
Run Code Online (Sandbox Code Playgroud) 我有点困惑,因为我确信这应该有所不同.看看这个代码示例:
#include <iostream>
#include <string>
using namespace std;
class base
{
public:
virtual ~base() = default;
};
class derived : public base
{
private:
int a = 0;
int *b = nullptr;
std::string lol;
public:
derived(std::string s) : b(new int(6)), lol{s} { cout << "ctor " << lol << endl; }
derived(derived const& d) : lol{d.lol + " copy"} {cout << "copy " << lol << endl; }
virtual ~derived() { cout << "dtor " << lol << endl; delete …Run Code Online (Sandbox Code Playgroud) 我在应用程序周围的各个位置使用了dotdict来增强代码的可读性。我几乎不知道这会在以后引起很多问题。一个特别烦人的情况是它似乎与副本库不兼容。
这就是我的意思
class DotDict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
Run Code Online (Sandbox Code Playgroud)
即这样访问字典属性的方法: dictionary.attribute
当我尝试
nested_dico = DotDict({'example':{'nested':'dico'}})
copy.deepcopy(nested_dico)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
167 reductor = getattr(x, "__reduce_ex__", None)
168 if reductor:
--> 169 rv = reductor(4)
170 else:
171 reductor = getattr(x, "__reduce__", None)
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
我认为这是因为它无法识别我的类DotDict,因此将其视为NoneType。
有谁知道解决这个问题的方法吗?也许重写副本库的有效类型?
deep-copy ×10
c++ ×6
.net ×2
c# ×2
arrays ×1
binary-tree ×1
c++11 ×1
clone ×1
containers ×1
copy ×1
icloneable ×1
ios ×1
iphone ×1
memcpy ×1
nested ×1
object ×1
objective-c ×1
polymorphism ×1
python ×1
python-3.x ×1
shallow-copy ×1
std ×1
structure ×1