fguihen Posted April 6, 2005 Posted April 6, 2005 i have looked at operator overloading, and can find out how to overload everything except the = operator. i have my own vector class, which has an x and a y co ordinate. rather than going v1.x = v2.x; v1.y = v2.y; id rather be able to do this: v1 = v2; any ideas? Quote
Wile Posted April 6, 2005 Posted April 6, 2005 What you can do is create a copy constructor (well, it was called the copy constructor in C++ ;) ) So you can do: v1 = new Vector(v2); Now you have to create a constructor that accepts a parameter of type Vector as only parameter, in that constructor you create a (deep) copy of the element. Something like public Vector(Vector original) { this.x = original.x; this.y = original.y; } Quote Nothing is as illusive as 'the last bug'.
mskeel Posted April 6, 2005 Posted April 6, 2005 assignment on classes does shallow copy free, yes? I believe doing just Vector v1 = v2; will work as a shallow assignment, but those are extremely dangerous and not very useful. The copy constructor will allow you to create a deep copy (assuming you code it correctly) so that v1 and v2 are equal but point to different places in memory. Wile outlined the goods you should use assuming that's what your vector class looks like. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.