• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

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

## Recommended Posts

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]

##### Share on other sites
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...

##### Share on other sites
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]

##### Share on other sites
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!

##### Share on other sites
>>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[i].Draw();

There are plenty of ways of doing it... just pick one and do it like that

##### Share on other sites
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?

##### Share on other sites
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.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978617

• 9
• 13
• 12
• 10
• 12