473,706 Members | 2,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep copy of struct

I have a struct like this:
public struct Vec
public float[] m;
public Vec(float v1, float v2)
m = new float[2];

public Vec operator+(Vec v1, Vec v2)
Vec r = new Vec(0,0);
r.m[0] = v1[0]+v2[0];
r.m[1] = v1[1]+v2[1];
return r;

Then I try something like this:
Vec v1 = new Vec(1,1);
Vec v2 = new Vec(2,2);
Vec v3 = v1 + v2;
v3.m[0] =0; // v1.m[0] is still 1, v2.m[0] is still 2

however, when trying something like:
Vec v1 = new Vec(1,1);
Vec v3 = v1;
v3.m[0] = 0; // v1.m[0] is 0!!!

In first case, + operator creates new object with new "m" array, so changes
in v3 doesn`t affect v1 or v2... In second case, shalow copy is performed,
so v1 and v3 share same "m" array... Question is, how do I create deep copy
of struct, so I can use assignment operator (=)? I could use copy
constructor, but then I will have to write:
Vec v3 = new Vec(v1);
which is ugly...
Nov 25 '05 #1
1 3630
Miljana Murgas <mi*****@hotmai l.com> wrote:
I have a struct like this:


You've basically given a good example of why mutable structs should be
avoided - and in particular why structs which contain *mutable
reference types (such as arrays)* should be avoided. I suggest you make
it immutable, with any calls which *would* change the contents actually
just returning a new one.

Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 26 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Ace Alexander | last post by:
I searched the internet and this news group and am still having problems grasping this concept. I have: struct file { bool open; vector<file*>my_files; }; I initialize a file: file main_file;
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the assignment Student* john = michael; both john and michael share the same object. This type of an assignment is different then value-assignmnet semantics used by class variables, as in
by: Justin Robbs | last post by:
I am trying to write the communcations part of a Point of Sale program for the Convenience Store industry. The setup in each store will have varying numbers of registers. There could be as few as 2 or as many as 12. The program I am working on runs on a computer which communicates to our gas pumps and sends status changes to all registers. It also handles a certain amount of individual communications to a specific register. Anyway, I...
by: marcosegurini | last post by:
Hi, the following class describes my actual problem: class DeepCopy<T> { T val_; public void Assign(T newVal) {
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's Collection classes does not support deep copy methods by theself, only Clone with shallow copy.
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
by: bob | last post by:
hi, I have been tasked with transferring a rather complex, deeply nested structure across dll's / memory spaces and effectively I need to deep copy the structure in question. Its a C struct and it contains pointers. I would have liked to think I could copy construct it, but I can't. I need to follow the pointers. Does anybody know if there's a quick and easy way to achieve this (as opposed to manually following the pointers and fields...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.