problem with arrays

I have those simple Classes :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

class ms{
public:
	ms(){cout<<"ms built";};
	~ms(){};

};


class mm{
public:
	mm(){cout<<"mm built";};
	~mm(){};

	static int mm_arr[12];
	static  void 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]
Why the output is "1" when the array for 'maroun' is '0' ??
because mm_arr is static and exists just once. It doesn't matter how you access it
Thank you very much !! got it :)
Topic archived. No new replies allowed.