--> 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)
|