deep-copy

copy constructor of a class which has self-pointer to itself in C++?

懵懂的女人 提交于 2019-12-04 18:39:34
I wanted to ask that how will we implement a copy constructor of a class which has self pointer to itself as its data member, i want to implement a deep copy, class City { string name; City* parent; public: City(string nam, double dcov); City(string nam, double dcov, City* c); City(const City& obj) { this-> name = obj.name; // how to assign parent parent = new City(??) } ~City(); void setName(string name1); void setDistanceCovered(int dist); string getName(); double getDistanceCovered(); City* getParent(){return parent;} }; I am confused that this line // how to assign parent parent = new City

When does pandas do pass-by-reference Vs pass-by-value when passing dataframe to a function?

谁都会走 提交于 2019-12-04 17:18:12
def dropdf_copy(df): df = df.drop('y',axis=1) def dropdf_inplace(df): df.drop('y',axis=1,inplace=True) def changecell(df): df['y'][0] = 99 x = pd.DataFrame({'x': [1,2],'y': [20,31]}) x Out[204]: x y 0 1 20 1 2 31 dropdf_copy(x) x Out[206]: x y 0 1 20 1 2 31 changecell(x) x Out[208]: x y 0 1 99 1 2 31 In the above example dropdf() doesnt modify the original dataframe x while changecell() modifies x. I know if I add the minor change to changecell() it wont change x. def changecell(df): df = df.copy() df['y'][0] = 99 I dont think its very elegant to inlcude df = df.copy() in every function I

How to perform deep copying of struct with CUDA? [duplicate]

夙愿已清 提交于 2019-12-04 11:50:35
问题 This question already has answers here : Copying a struct containing pointers to CUDA device (3 answers) Closed last year . Programming with CUDA I am facing a problem trying to copy some data from host to gpu. I have 3 nested struct like these: typedef struct { char data[128]; short length; } Cell; typedef struct { Cell* elements; int height; int width; } Matrix; typedef struct { Matrix* tables; int count; } Container; So Container "includes" some Matrix elements, which in turn includes some

Why doesn't the .NET framework provide a method to deep copy objects? [closed]

跟風遠走 提交于 2019-12-04 05:43:11
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I was using a custom method to deep clone objects the other day, and I know you can deep clone in different ways (reflection, binary serialization, etc), so I was just wondering: What is/are the reason(s) that Microsoft does not include

Why is the Object class's clone() method giving a deep copy of object?

喜夏-厌秋 提交于 2019-12-04 04:45:46
问题 As per the JAVA documentation, the super.clone() when called returns a shallow copy of the object. In the code below I have two objects name and id ; and one primitive variable num . When the super.clone() method is called on the first object, it seems to be creating a deep copy of the objects(name and id) in addition to an expected copy of only num. After cloning the object obj, I have changed its name and id fields. These changes should be reflected in the cloned object if a shallow copy

C# Automatic deep copy of struct

醉酒当歌 提交于 2019-12-04 02:56:06
问题 I have a struct, MyStruct , that has a private member private bool[] boolArray; and a method ChangeBoolValue(int index, bool Value) . I have a class, MyClass , that has a field public MyStruct bools { get; private set; } When I create a new MyStruct object from an existing one, and then apply method ChangeBoolValue(), the bool array in both objects is changed, because the reference, not what was referred to, was copied to the new object. E.g: MyStruct A = new MyStruct(); MyStruct B = A; /

Creating an easy to maintain copy constructor

冷暖自知 提交于 2019-12-04 00:29:05
Consider the following class: class A { char *p; int a, b, c, d; public: A(const &A); }; Note that I have to define a copy constructor in order to do a deep copy of "p". This has two issues: Most of the fields should simply be copied. Copying them one by one is ugly and error prone. More importantly, whenever a new attribute is added to the class, the copy constructor needs to be updated, which creates a maintenance nightmare. I would personally like to do something like: A(const A &a) : A(a) { // do deep copy of p ::: } So the default copy constructor is called first and then the deep copy is

Does a copy constructor/operator/function need to make clear which copy variant it implements?

寵の児 提交于 2019-12-03 23:55:12
问题 Yesterday I asked a question about copying objects in C#, and most answers focussed on the difference between deep copy and shallow copy , and the fact that it should be made clear which of both copy variants a given copy constructor (or operator, or function) implements. I find this odd. I wrote a lot of software in C++, a language that heavily relies on copying, and I never ever needed multiple copy variants. The only kind of copy operation I ever used is the one I call " deep enough copy "

Copy constructor: deep copying an abstract class

元气小坏坏 提交于 2019-12-03 23:48:54
Suppose I have the following (simplified case): class Color; class IColor { public: virtual Color getValue(const float u, const float v) const = 0; }; class Color : public IColor { public: float r,g,b; Color(float ar, float ag, float ab) : r(ar), g(ag), b(ab) {} Color getValue(const float u, const float v) const { return Color(r, g, b) } } class Material { private: IColor* _color; public: Material(); Material(const Material& m); } Now, is there any way for me to do a deep copy of the abstract IColor in the copy constructor of Material? That is, I want the values of whatever m._color might be

Invert linear linked list

余生颓废 提交于 2019-12-03 22:45:30
a linear linked list is a set of nodes. This is how a node is defined (to keep it easy we do not distinguish between node an list): class Node{ Object data; Node link; public Node(Object pData, Node pLink){ this.data = pData; this.link = pLink; } public String toString(){ if(this.link != null){ return this.data.toString() + this.link.toString(); }else{ return this.data.toString() ; } } public void inc(){ this.data = new Integer((Integer)this.data + 1); } public void lappend(Node list){ Node child = this.link; while(child != null){ child = child.link; } child.link = list; } public Node copy(){