#include <iostream>
usingnamespace std;
class ms{
public:
ms(){cout<<"ms built";};
~ms(){};
};
class mm{
public:
mm(){cout<<"mm built";};
~mm(){};
staticint mm_arr[12];
staticvoid print_name();
};
// A stack of ints that cannot hold 0s.
class marco {
public:
marco();
~marco();
static mm maroun;
static mm mar;
};
the main :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main() {
int i;
marco momin;
for(i=0;i<12;i++)
{
marco::mar.mm_arr[i]=0; // 0 in array
marco::maroun.mm_arr[i]=0;
}
marco::mar.mm_arr[3]=1; // gave '1' for a place in array
cout<<marco::maroun.mm_arr[3];
//WHAT ??!! the second array was updated too, why ?
return 0;
}
Why the output is "1" when the array for 'maroun' is '0' ??
You have only one static array for all objects of class mm because this array is static. So there is no any difference between access to it through object mar or object maronn. It is equivalent to record mm:mm_arr[i]