Jump to content
  • Advertisement
Sign in to follow this  
cordel

[java] This , Copy-constructor and what's between

This topic is 3543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello , I'd like your help with the following problem. I have a class called "ShapeContain" and I've come to a point where it is needed to use a copy constructor. The problem is that after making a temporary ShapeContain
ShapeContain tempShape=new ShapeContain(this);


and placing in it the "this" information:
for (i=0; i<this.arr.length ; i++) 
   tempShape.arr=this.arr;


and doing a few more array-commands ,I cannot place the temporary in "this" .Is it even possible ? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
There's nothing wrong with using the keyword 'this' in the circumstance you describe. It's difficult to tell with the given information.

I did notice one thing however: Your length may change each iteration.
Did you perhaps mean "this.arr.length" instead of "this.arr.length"?

Share this post


Link to post
Share on other sites
That is not how a copy constructor is meant to be used. A copy construct is used for creating a new object based on an already existing one. So far, so good.

Unless I misunderstand what you are doing, the difference between how a copy constructor is supposed to be used, and how you are doing it, is that the copying is supposed to be done in the new object's constructor, not in the old object's methods.

public ShapeContain(ShapeContain other) {
arr = other.arr.clone();
...
}


So then copying becomes just:

ShapeContain newShape = new ShapeContain(this/existingShape/whatever);


You still cannot use it to replace the this-reference in an object's methods, though. But why would you need to do that?

Since you just copied this, you have a (possibly exact) copy, so why do you want to replace it with a copy? If there is a difference between the new one and the old one, why not just change the existing copy yourself (doing what would otherwise be done in the copy constructor)?

Share this post


Link to post
Share on other sites
Okay people I've solved that problem but I've a new one (:

I want to create a dynamic array using a copy constructor.I have a 10 cell array of OBJECT (when the object has 2 fields: rectangle and triangle),
but when I'm trying to create 10 more cells (which means: the previous amount + 10 more) the eclipse throws an error. where is my mistake ?



public class ShapeContainer {

public static final int INIT_SIZE=10;
public static final int RESIZE=10;

public class Pair
{
public Rectangle rec;
public Triangle tri;
}

Pair[] allPairs; // Array of 'Pair' objects
int [] tricount,reccount;

//////////////////////// Constructor /////////////////////////

public ShapeContainer (ShapeContainer other) { // Copy constructor for ShapeContainer.

other.allPairs=new Pair[this.allPairs.length + RESIZE];
other.reccount=new int[this.reccount.length + RESIZE];
other.tricount=new int[this.tricount.length + RESIZE];
}

//////////// method ////////

public void add(Rectangle r) { // Add the given rectangle to the container

ShapeContainer tempShape=new ShapeContainer(this);

}





When I try to make a temporary 'ShapeContainer' with tempShape , it goes into the copy constructor ,and for some reason it stucks there and throws an error. What am I doint wrong ? thanks !

Share this post


Link to post
Share on other sites
Quote:
Original post by cordel
Okay people I've solved that problem but I've a new one (:

I want to create a dynamic array using a copy constructor.I have a 10 cell array of OBJECT (when the object has 2 fields: rectangle and triangle),
but when I'm trying to create 10 more cells (which means: the previous amount + 10 more) the eclipse throws an error. where is my mistake ?

*** Source Snippet Removed ***

When I try to make a temporary 'ShapeContainer' with tempShape , it goes into the copy constructor ,and for some reason it stucks there and throws an error. What am I doint wrong ? thanks !


As far as I can tell you have your constructor a little backwards... Are you trying to create a new ShapeContainer based on an existing one? If so you should be doing this:


public ShapeContainer (ShapeContainer other) { // Copy constructor for ShapeContainer.

allPairs=new Pair[other.allPairs.length + RESIZE];
for (int i=0; i<others.allPairs.length; i++)
allPairs = new Pair(other.allPairs); // Since Pair is an object...
reccount=new int[other.reccount.length + RESIZE];
for (int i=0; i<others.reccount.length; i++)
reccount = other.reccount;
tricount=new int[other.tricount.length + RESIZE];
for (int i=0; i<others.tricount.length; i++)
tricount = other.tricount;

}



Note that you need a "copy constructor" for Pair since it is an object. Otherwise I'm a little confused by your add(Rectangle r) method...


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!