I have a struct like this:
public struct Vec
{
public float[] m;
public Vec(float v1, float v2)
{
m = new float[2];
m[0]=v1;
m[1]=v2;
}
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... 1 3628
Miljana Murgas <mi*****@hotmai l.com> wrote: I have a struct like this:
<snip>
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 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: 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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |