Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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;
}

Nothing is as illusive as 'the last bug'.
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...