Can someone explain this code for me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// static members in classes
#include <iostream>
using namespace std;

class CDummy {
  public:
    static int m;
    static int n;
    CDummy () { n++;};
    ~CDummy () { n--; };
};

int CDummy::n=0;

int main () {
  CDummy a;
  CDummy b[5];
  CDummy * c = new CDummy;
  cout << a.n << endl;
  delete c;
  cout << CDummy::n << endl;
  system("pause");
  return 0;
}


This is the code i copy from the tutorial, there are few things which i am not sure what they do.

1
2
3
4
5
6
7
CDummy a; // the class has a constructor, so when declared, the n is added by 1?

CDummy b[5];// this is add n by 5? This is very confusing...

CDummy * c = new CDummy; // So the constructor will also run when a pointer 
                         // of the class is declared?
1
2
3
4
5
6
7
8
9
10
11
12
CDummy a; // the class has a constructor, so when declared, the n is added by 1?

yes

CDummy b[5];// this is add n by 5? This is very confusing...

it creates a 5 element array

CDummy * c = new CDummy; // So the constructor will also run when a pointer 
                         // of the class is declared?

thats simply a dynamic object created with "new"
CDummy b[n];

if I can change the n during runtime, does that mean i can create new object during the runtime?
No, the other way round. You can create new objects during runtime (with new) and the constructor of the object will indirectly increase the n. The n variable shouldn't be public but private so it can only be accessed by functions of the class (in this case usually constructor and destructor).

CDummy b[n]; would be wrong for that
CDummy* myPointer = new CDummy[k];
would be correct where k is the amount of objects u want to have.
Since the constructor of each object increases n by one the above statement will increase n by k

mfG
Machtl
Last edited on
Ok, got it, thanks everyone
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
// static members in classes
#include <iostream>
using namespace std;

class CDummy {
  public:
    int m;
    static int n;
    CDummy () { n++;};
    ~CDummy () { n--; };
};

int CDummy::n=0;

int main () {
  CDummy * a = new CDummy[2];
  
  (*a).m = 100;
  (*(a[1])).m = 200;
  
  cout << (*a).m <<" " << (*a[1]).m;
  cout << endl;
  system("pause");
  return 0;
}


But if i edit a the code a bit, and i create 2 object with :
 
CDummy * a = new CDummy[2];


How would i access the second object's m value?
Oh never mind figured it out,
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
// static members in classes
#include <iostream>
using namespace std;

class CDummy {
  public:
    int m;
    static int n;
    CDummy () { n++;};
    ~CDummy () { n--; };
};

int CDummy::n=0;

int main () {
  CDummy * a = new CDummy[2];
  
  (*a).m = 100;
  (*(a+1)).m = 200;
  
  cout << (*a).m <<" " << (*(a+1)).m;
  cout << endl;
  system("pause");
  return 0;
}


Just relise those object on different part of the memory
For what it's worth... you are using the confusing pointer syntax. It's much simpler if you use the [brackets]

1
2
3
4
5
6
7
8
// this code:
  (*a).m = 100;
  (*(a+1)).m = 200;


// is the same as this code:
a[0].m = 100;
a[1].m = 200;



Most people find the latter form to be much cleaner and easier to understand.
Other than making multiple declaration of the class, how do we use the CDummy b[n]; ?
Topic archived. No new replies allowed.