Class issues... Thing[1] = Thing[29] ??

Started by
5 comments, last by SpaceDude 20 years ago
Hi there!! I'm having a problem with my classes... I just switched to using classes, and in structs it was okay to do: Thing[1] = Thing[29] etc...

class thing
{
public: 
//variables...

}

thing *SpawnThing[20];
thing *Thing[20];



void main()
{
/*
(note: of course have I initialised the Things with "new" first...) 
*/

Thing[1] = Thing[19];
//....  whats wrong with this??? =/


}

 
Isn't it possible to do ^^ with classes? It seems that the rest of the "Things" becomes faulty after I've made the copy, etc if I got three houses drawn, and the third is = SpawnThing[19], the first house disappears... (???) weird issue.. If this is the wrong way to copy all states of a thing, what is the RIGHT way of doing this? =/ It would be a real hassle if the above made copy won't work.. cuz manually assigning ~150 properties is a pain in da butt. Thanks people! [edited by - Rasmadrak on April 5, 2004 6:24:28 PM]
"Game Maker For Life, probably never professional thou." =)
Advertisement
try this:

*(Thing[1]) = *(Thing[19]);

so that you actually copy the contents, rather than the pointer... if you just copy the pointer you will have Thing[1] and Thing[19] pointing to the same location in memory...
I want an array of 20 individual things, am I doing my "news" wrong?

thing *Thing[20];for (int i =0; i < 20; i++){   Thing[i] = new thing;}void render(){    for (int i =0; i < 20; i++)    {       Thing[i]->Draw();    }};


I´m not that good at using classes thou, so I´m probably wrong.


If I do: thing *Thing = new[20] ;

does this make each Thing individual? And can I use arrays with them?
i. e:

for (int i=0; i < 20;i++){   Thing[i]->Draw();  //is this possible?}


"Game Maker For Life, probably never professional thou." =)

[edited by - Rasmadrak on April 6, 2004 8:54:21 AM]
"Game Maker For Life, probably never professional thou." =)
Well, your news and such look correct, assuming you actually need dynamically allocated things. But when you do Thing[1] = Thing[19]; it''s only going to copy pointer values. You''re not going to actually turn Thing[1] into a copy of Thing[19] in the way that you expect, but instead, you''re just saying that the pointer Thing[1] is now pointing to whatever Thing[19] is also pointing at, and whatever Thing[1] was pointing at, is now gone forever (but still there; you just don''t know where it is, i.e., dangling pointer. Thus, you can''t delete it. Thus, memory leak.)

If I were you (and not knowing too much about your project), I''d just create an array of Thing[20], not *Thing[20], and then you wouldn''t have to worry about new, delete, copying pointers, or any of that mess. I have a feeling that you''re not as comfortable with pointers as you maybe should be. Either quit using them for now, since I''m not sure this program needs them, or study them in more detail, so you know what is actually going on specifically.

Good luck!
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
>>If I do: thing *Thing = new[20] ;

you want that to be: thing *Thing = new thing [20];
yes it will create an array of 20 things...

>>for (int i=0; i < 20;i++)
>>{
>> Thing->Draw(); //is this possible?
>>}

you want that to be: Thing.Draw();<br><br>There are plenty of ways of doing it… just pick one and do it like that <img src="smile.gif" width=15 height=15 align=middle> </i>
Hehe, ok. =)

You´re right, I''m not used to using classes, in fact - this is my first attempt at using them on a higher level..

But anyway, I found that they´re great, but since I''m self educated in programming I might not have learned the stuff from the basics and up... :/

Anyway, I think that making an array of 5000 or more Things would take up a lot space in memory?

This is a RTS game, so I will be dealing with a great deal of objects, so I figured the best way would be to dynamicly create them?
"Game Maker For Life, probably never professional thou." =)
that seems fair enough... you may want to look into linked lists, pretty usefully for storing data where you don''t know ahead of time how many items you will need.

This topic is closed to new replies.

Advertisement