deep-copy

Cloning objects in C#

回眸只為那壹抹淺笑 提交于 2019-12-05 12:26:39
I defined next class with virtual properties: public class Order: BaseEPharmObject { public Order() { } public virtual Guid Id { get; set; } public virtual DateTime Created { get; set; } public virtual DateTime? Closed { get; set; } public virtual OrderResult OrderResult { get; set; } public virtual decimal Balance { get; set; } public virtual Customer Customer { get; set; } public virtual Shift Shift { get; set; } public virtual Order LinkedOrder { get; set; } public virtual User CreatedBy { get; set; } public virtual decimal TotalPayable { get; set; } public virtual IList<Transaction>

Does LINQ new up memory when creating returns

狂风中的少年 提交于 2019-12-05 09:02:36
Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original? It's going to depend on if (and how) you use Select to project the results. If you do not create new objects in a projection then the result will reference the same objects as the original collection. If, however, you create new objects in the project then, obviously, they will not be the same. The collection returned here will contain references to the same objects in _myCollection : from m in _myCollection where m

How to deep copy QMap and other Qt containers

余生颓废 提交于 2019-12-05 04:35:49
Generally speaking, what is the correct way to deep copy Qt containers? I'm not worried about deep copying the containers recursively, although addressing such would be helpful. Despite what everyone will tell you - that you don't deep copy Qt containers - there are situations in which you simply need to perform an actual deep copy instead of just a shallow one. To do that, use detach() : container1 = container2; container1.detach(); 来源: https://stackoverflow.com/questions/16800206/how-to-deep-copy-qmap-and-other-qt-containers

Java - Implement deep and shallow copy of an array

一曲冷凌霜 提交于 2019-12-05 03:18:41
I am trying to understand the concept of shallow vs deep copy in Java. There is a lot of articles and Q&A about this subject, but whenever I try to implement these concepts in a real Java code, everything become unclear to me. One of the answers on which I base my understanding is in this link , where deep and shallow copying are explained via schemas. I will show you below my implementation for each case: Shallow copy: I took for my example the method System.arraycopy() as I read in many articles that it performs a shallow copy (along with the clone method) public class Test { public static

Alternative of strcpy in c++

痞子三分冷 提交于 2019-12-05 03:00:49
In C i used strcpy to make a deep copy of a string, but is it still 'fine' to use strcpy in C++ or are there better alternatives which i should use instead ? In C++ the easiest way is usually to use the std::string class instead of char*. #include <string> ... std::string a = "Hello."; std::string b; b = a; The line "b = a;" does the same thing you would usually do with strcpy. I put this in the comment above, but just to make the code readable: std::string a = "Hello."; std::string b; b = a.c_str(); // makes an actual copy of the string b = a; // makes a copy of the pointer and increments the

How to deep clone the state and roll back in Vuex?

限于喜欢 提交于 2019-12-05 01:40:29
问题 In Vuex I would like to take a snapshot / clone of an object property in the tree, modify it, and later possibly roll back to the former snapshot. Background: In an application the user can try out certain changes before applying them. When applying the changes, they should effect the main vuex tree. The user can also click «cancel» to discard the changes and go back to the former state. Example: state: { tryout: {}, animals: [ dogs: [ { breed: 'poodle' }, { breed: 'dachshund' }, ] ] } User

How do you perform a deep copy of a struct in Go?

南笙酒味 提交于 2019-12-05 01:17:53
I'm attempting to perform a deep copy of the following struct: // Ternary Tree type Tree struct { Left *Tree Mid *Tree Right *Tree Value interface{} Parent *Tree Orientation string IsTerminal bool Type string } The following is my sorry attempt. It looks like I'm creating a new tree at the root but it's children are still pointing to the same address in memory. func (tree *Tree) CopyTree() *Tree { if (tree == nil) { return nil } else { copiedTree := &Tree { tree.Left.CopyTree(), tree.Mid.CopyTree(), tree.Right.CopyTree(), tree.Value, tree.Parent.CopyTree(), tree.Orientation, tree.IsTerminal,

Call default copy constructor from within overloaded copy constructor

时光总嘲笑我的痴心妄想 提交于 2019-12-05 01:10:20
I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo(Foo const & other) { p.reset(new Bla(*other.p)); // Can I avoid having to write the default copy

Creating clone of an object not working with virtual base class

流过昼夜 提交于 2019-12-04 22:25:41
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<"Something initialized. j="<<j<<endl;} }; class Base { private: Base(const Base&) {} public: Base() {} virtual Base *clone() { return new Base(*this); } virtual void ID() { cout<<"BASE"<<endl; } }; class Derived : public Base { private: int id; Something *s; Derived(const Derived&) {} public: Derived():id(10) {cout<<"Called constructor and allocated id"<<endl;s=new Something();} ~Derived() {delete s;} virtual Base *clone() { return new Derived(*this); } virtual void ID() { cout<<"DERIVED id="<<id<

std::shared_ptr deep copy object

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 18:48:53
问题 Can't find much on that for C++11 but only on boost. Consider the following class: class State { std::shared_ptr<Graph> _graph; public: State( const State & state ) { // This is assignment, and thus points to same object this->_graph = std::make_shared<Graph>( state._graph ); // Deep copy state._graph to this->_graph ? this->_graph = std::shared_ptr<Graph>( new Graph( *( state._graph.get() ) ) ); // Or use make_shared? this->_graph = std::make_shared<Graph>( Graph( *( state._graph.get() ) ) )