Date: Wed, 30 Jul 1997 23:08:41 -0400 (EDT) From: stunajoh AT acs DOT eku DOT edu Subject: Re: Accessing Variables In-reply-to: <33DFC2DB.356A@lr.net> To: Isaac Waldron Cc: djgpp AT delorie DOT com Message-id: MIME-version: 1.0 Content-type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Wed, 30 Jul 1997, Isaac Waldron wrote: > Does anyone know if it is possible to create a variable in a function, > and then access it outside of that function. For instance, I have > classes Ship and Shot, can I use code like this: > It seems to me that what you want could be accomplished having a pointer to a shot in ship, and in the Fire() function have it create a new shot gun (on the heap) if it hasn't been created yet (sort of like in your ship::update(), but use the new operator instead. ). However, unless you call this pointer a static, you will need to reference it by gun::pFirst->Update() (I'm pretty sure that it has to be this way. I do know that this at least works.). In the default(non-static), you will have a pointer and an instance of Shot for each instance of Ship. However, if this pointer is static, there will be only one instance for the entire class Ship, no matter how many instance variables( at least the way you plan to set it up ). Maybe I missed the point entirely on what you were asking, and maybe this doesn't apply to you. Just trying to help. Also, remember to delete your instance(s) of Shot, probably in the destructor of Ship. > Ship::Fire() > { > Shot gun(x + 20, y + 8); //constructor takes x and y for shot > }; > > Ship::Update() > { > if (Shot::pFirst) > { > Shot::pFirst->Update() > }; > }; > > Basically, I need to know if a variable constructed in a function can be > made to not be destructed at the end of that function, and have it > visible globally? If not, does anyone know how to make a linked list of > objects so I can have more than one object of any one type on screen at > one time? > > thanks in advance, > -- > -Begin Signature- > Isaac Waldron N1YZI > http://www.geocities.com/SiliconValley/Lakes/5703/home.html > -End Signature- >