pointers to objects

Started by
9 comments, last by simon10k 17 years, 12 months ago
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.

int main()
{
    Game* game = new Game;
    game->run();
    delete game;
}

// Why not just do

int main()
{
    Game game;
    game.run(); //edit: oops
}


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]
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Advertisement
actually that would be
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

Beginner in Game Development?  Read here. And read here.

 

EDIT: Yeah, just follow the link Alpha posted instead.
It only takes one mistake to wake up dead the next morning.
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.
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)

Beginner in Game Development?  Read here. And read here.

 

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!
It only takes one mistake to wake up dead the next morning.
Quote:Original post by Frequency
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!


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

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]
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
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. :(
It only takes one mistake to wake up dead the next morning.

This topic is closed to new replies.

Advertisement