Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / dynamic memory thing?

Author
Message
RancidRat
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 27th Jul 2008 22:11 Edited at: 27th Jul 2008 22:16
Hi
I am a noob and learning C++ and DGDK. I am trying to figure out how to use pointers and dynamic memory things.
I was wondering if this is possible, or how I should go about it:

Say I have a class for making a monster (in this case a sphere for example sake) in a game:



now to make a monster I can say : CMonster monster(100,100);

and to run its moving code : monster.run();

I know I can have an array without the constructer:
CMonster monster[50];
monster.setup(100,100);

and then use: monster[1].run()

Here is my question: If I use the array, I am limited to how many "monsters" I can make in my program. What I want to know is: is it possible to use dynamic memory stuff to make new "monsters" and run them whenever I need to (e.g. a RTS game)? something like:

new CMonster (100,100);

and then also to use the monster.run() command.

Is this possible or am I missing the plot and being silly? And how would you point to the new "monsters"? Is there a simpler way I am not seeing?

Thanks
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 27th Jul 2008 22:18
I don't think you can supply parameters if it's declared with new.

But, that aside, you can't declare a name for it. You have to assign the new command to a pointer. For example,



Then, since it's a pointer, you can only access it's member functions like so:



Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
RancidRat
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 27th Jul 2008 22:31 Edited at: 27th Jul 2008 22:41
Yeah...thanks that makes sense.. very helpful
cus then I can just say:

monster->run();

Thanks alot, Ill play around with that.

I saw this example:


I was looking at that


and that made me wonder if you could constantly change, having different numbers of "monsters" using the run code:

monster[ x ] -> run ( );

where x could change (if that makes sense). Thanks for your help
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 27th Jul 2008 22:58
That will work, but you must be careful. Depending on the memory requirements of the class, it can add up fast.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Jul 2008 23:52
Take a look at the C++ vector class - it provides pretty much the same functionality as an array, and variable sizing too.

Login to post a reply

Server time is: 2024-09-30 03:33:24
Your offset time is: 2024-09-30 03:33:24