int main()
{
Game* game = new Game;
game->run();
delete game;
}
// Why not just do
int main()
{
Game game;
game.run(); //edit: oops
}
pointers to objects
Reading another post in this forum just trigger a thought, why do people create pointers to objects when there is no obvious reason to do so. If there is please enlighten me.
I see examples like this all the time, there are obviously no virtual functions involved so why do it. It makes code look ugly and your have to explicitly delete the object when you’re finished.
[Edited by - simon10k on April 22, 2006 12:13:44 PM]
actually that would be
why you would put the game or game engine as a pointer, i don't know, but i'll link to some other explanations that's popped up in the last hour.
http://www.gamedev.net/community/forums/viewreply.asp?ID=2572448
Game game;game.run();
why you would put the game or game engine as a pointer, i don't know, but i'll link to some other explanations that's popped up in the last hour.
http://www.gamedev.net/community/forums/viewreply.asp?ID=2572448
If the game is large, then it will be always taking up that amount of stack space. If you want to reduce the amount of space taking up the stack you can put it on the heap.
But I'm not sure thats going to be a problem most of the time.
But I'm not sure thats going to be a problem most of the time.
Quote:
That's part of the reason I use pointers - another is that you have to use the default constructor if you make member objects of a class on the stack
Huh?
Have you ever tried:
Object object( arg1, arg2, arg3 );
Quote:Original post by rip-off
If the game is large, then it will be always taking up that amount of stack space. If you want to reduce the amount of space taking up the stack you can put it on the heap.
But I'm not sure thats going to be a problem most of the time.
well speaking in terms of consoles (ie. Dreamcast, PS2, Xbox, GC), how is the RAM split between stack memory and heap memory? is there a split or the program just uses memory 'til it runs out?
(sorry for the hijack)
Quote:Original post by rip-off
Huh?
Have you ever tried:
Object object( arg1, arg2, arg3 );
Yes I have; it was a long time ago when I sucked even more at C++ and I could never get it to work. I suppose you'll also want to construct inside the owner-class's constructor via pointer if it uses the parameters of the owner-class constructor, but if what you just wrote works fine I feel pretty silly.
Anyway, it's reasons like this that I edit out my post and let smarter folks answer!
Quote:Original post by FrequencyQuote:Original post by rip-off
Huh?
Have you ever tried:
Object object( arg1, arg2, arg3 );
Yes I have; it was a long time ago when I sucked even more at C++ and I could never get it to work. I suppose you'll also want to construct inside the owner-class's constructor via pointer if it uses the parameters of the owner-class constructor, but if what you just wrote works fine I feel pretty silly.
Anyway, it's reasons like this that I edit out my post and let smarter folks answer!
You can use initialiser lists for that...
class OwnerClass{ Object member; OwnerClass( int arg1, int arg2, int arg3 ) : member( arg1, arg2, arg3 ) { }};
I would guess the stack and heap both use RAM. And the stack memory is released when the name goes out of scope. Correct me if I'm wrong, we dont want the blind leading the blind ;)
so
Also, does this answer my question why people might use the heap for this?
[Edited by - simon10k on April 22, 2006 12:17:40 PM]
so
int main(){ Game* p = new Game; p->run(); delete p; //The game memory has been freed, do something else /* ... */}//...int main(){ Game p; p.run(); //The game object still exists, so less memory to use /* ... */}
Also, does this answer my question why people might use the heap for this?
[Edited by - simon10k on April 22, 2006 12:17:40 PM]
Quote:Original post by rip-off
You can use initialiser lists for that...
*** Source Snippet Removed ***
That, sir, is clever.
edit: I've already r++'d you in the past. :(
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement