0
    //static member in classes
#include <iostream>
using namespace std;

class CDummy {
public:
    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;
    return 0;
}

The result is 7, 6. Can anybody explain it for me? and I don't understand this "CDummy b[5];". People never use syntax like this in C, right? what is this here? Thank you!

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
  • 2
    I'd strongly recommend that you make sure you have [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list). – James McNellis Dec 31 '10 at 04:05
  • if this is a homework assignment or exercise it probably should be tagged as such – AJG85 Dec 31 '10 at 04:12

5 Answers5

2
CDummy b[5];

This declares an array of five CDummy objects. It ends up calling the CDummy default constructor five times (once for each object in the array).

You create seven CDummy objects: a, five in the array b, and the one pointed to by c. n then has a value of 7. Then you destroy one CDummy object (the one pointed to by c) and n has a value of 6. The remaining six CDummy objects are destroyed when they go out of scope when the main function returns.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

CDummy b[5] is an array of five CDummy objects. CDummy a is simply a single instance of CDummy, and so is CDummy c.

Every time a CDummy is created, the constructor is called.

Let's add that up: 5 + 1 + 1 = 7. That's why n was initially 7. When c was deleted, n-- was executed, and n became 6.

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
0

static on this situation mean that the variable n is the same for all instances of CDummy, in your case a, b and c (after you allocate the object using new) all share the same n. Thats why you see this value.

CDummy b[5] -> this declares an array of CDummy, on this case 5 CDummys.

bcsanches
  • 2,362
  • 21
  • 32
0

CDummy b[5] is just an array of 5 CDummy objects.

A static member means there is only one instance of that member no matter how many instances are made of the class.

The class simply increments n from the constructor whenever a new instance is initialized, and decrements n from the destructor whenever the instance is destroyed. It's basically tracking how many instances of this class are currently active.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

n is a static member variable in class CDummy. n is associated with all objects of the class rather than each object instance of the class. n is incremented as each object instance gets created and decremented as each object instance is destroyed. In the main function we create object a (n=1), followed by array of objects b of size 5 (n=6) followed by pointer to object c (n=7). So the first cout statement output 7. As object pointed to by c is destroyed n is decremented to 6. So the second cout statement outputs 6. Please refer to this link for details on static members in a class

Neera
  • 1,577
  • 8
  • 10