shallow-copy

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

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 "

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

限于喜欢 提交于 2019-12-03 08:54:34
This error occurs during run time, and I'm not sure what's causing it - the code looks correct to me. #include <iostream> #include <string> using namespace std; struct Room { int d_noSeat; bool d_hasProjector; Room() = default; Room(const Room& r); }; class Event { Room* d_room; std::string d_name; public: Event(); Event(const Event& e); ~Event(); void set(Room r, const std::string& name); void print(); }; Event::Event() : d_room(0), d_name("") {}; void Event::print() { std::cout << "Event: " << d_name; if (d_room != 0) { std::cout << " in size " << d_room->d_noSeat; if (d_room->d_hasProjector

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

拈花ヽ惹草 提交于 2019-12-02 02:05:45
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 was being made. Am I right? public class Cloning implements Cloneable { String name; int num; Integer id;

Why does copy of the List still change properties in the original List using C#

徘徊边缘 提交于 2019-12-02 01:38:25
Lets say I have this class public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public bool isActive { get; set; } } And use it like this: List<Employee> Employees = new List<Employee>(); Employees.Add(new Employee { FirstName = "firstname", LastName = "lastname", isActive = true }); List<Employee> EmployeesCopy = new List<Employee>(Employees); EmployeesCopy[0].isActive = false; Why does change in isActive property of EmployeesCopy also modify property in the original list? Because the new list still contains references to the same employee objects

Is shallow copy sufficient for structures with char[]?

旧巷老猫 提交于 2019-11-30 22:00:50
I have a structure containing character arrays with no any other member functions. I am doing assignment operation between two instances of these structures. If I'm not mistaken, it is doing shallow copy. Is shallow copy safe in this case? I've tried this in C++ and it worked but I would just like to confirm if this behavior is safe. Alok Singhal If by "shallow copy", you mean that after assignment of a struct containing an array, the array would point to the original struct 's data, then: it can't. Each element of the array has to be copied over to the new struct . "Shallow copy" comes into

VB.NET, Is Object Returned by Reference from Function

随声附和 提交于 2019-11-30 20:46:16
This should be a fairly common question, but I haven't found a straightforward answer anywhere. If I instantiate an object within a function in VB.NET and return it, does it return it by reference or by value. IE - should I be worried about performance if I write something like this: Public Function ret_obj_func() As big_object Dim ret_obj As New big_obj(<lots of stuff>) Return ret_obj End Function If I call this function from somewhere else, will it instantiate the object in the ret_obj and then create a deep copy to pass back a copy to the caller, Or will it just pass back a reference? It

Why are objects automatically passed by reference?

你说的曾经没有我的故事 提交于 2019-11-30 18:55:48
I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the method. However, at least objects passed as parameters to methods/constructors are behaving differently from the rest. It seems they are always passed by reference if no extra cloning is done as described here: http://zetcode.com/lang/csharp/oopii/ . Why are objects automatically passed by reference? Is there any particular benefit from forcing the

Is shallow copy sufficient for structures with char[]?

旧城冷巷雨未停 提交于 2019-11-30 17:22:16
问题 I have a structure containing character arrays with no any other member functions. I am doing assignment operation between two instances of these structures. If I'm not mistaken, it is doing shallow copy. Is shallow copy safe in this case? I've tried this in C++ and it worked but I would just like to confirm if this behavior is safe. 回答1: If by "shallow copy", you mean that after assignment of a struct containing an array, the array would point to the original struct 's data, then: it can't.

What is the difference between being shallowly and deeply equal? How is this applied to caching?

微笑、不失礼 提交于 2019-11-30 15:32:37
问题 Found the following in my notes, but I am unable to make sense of it: Primitive type wrapper classes implement caching for a limited number of values. This guarantees that a limited number of deeply equal wrapper objects are also shallowly equal: If o1.equals( o2 ) then o1 == o2 . For example, new Integer( 0 ) == new Integer( 0 ) . In general this does not always work. For example, new Integer( 666 ) == new Integer( 666 ) may not hold. The reason for caching is that it saves memory. In