TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JASON HENDRIKS
from: CLIFF RHODES
date: 1997-03-24 10:05:00
subject: Duration

--> Jason Hendriks wrote to Cliff Rhodes <--
JH> CR> class A {
JH> CR>   private:
JH> CR>      int array[100];
JH> CR> };
JH> 
JH>Is there a way to make the private members static in the
JH>class itself, even if an objected has automatic (dynamic)
JH>duration?
Yes, but the result may not be what you want. You could declare the 
array as a static member. Then you run into the problem that only a 
single copy of array exists for all instances of the class.
Another alternative to reduce stack usage would be to allocate array 
dynamically using new. Now data space for array comes from the heap, not 
the stack.
class A {
   private:
      int *array;
   public:
      A(int n = 100) { array = new int[n]; }
      ~A()  { delete [] array; }
};
JH>So this object's private members are NOT on the stack:
JH> 
JH>int main(void)
JH>{
JH>   static A b[50];
JH>};
Correct. Since b[] is a static array, the data members for all 50 
objects are in data memory, not on the stack.
X CMPQwk 1.42 1692 X"She never yet was foolish that was fair." - William 
Shakespeare
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.