delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/30/23:11:54

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 <waldroni AT lr DOT net>
Cc: djgpp AT delorie DOT com
Message-id: <Pine.PMDF.3.95.970730225431.630867C-100000@ACS.EKU.EDU>
MIME-version: 1.0


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 <waldroni AT lr DOT net> N1YZI
> http://www.geocities.com/SiliconValley/Lakes/5703/home.html
> -End Signature-
> 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019