static variable w/in a class

Hello,

In the Classes II portion of this site's excellent tutorials I encountered some things that I really had to research and hack on by commenting out and plugging in different variables.

I put the code below with comments that are actually questions. Am I on the right track with these comment/questions???

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
  // static members in classes
#include <iostream>
using namespace std;

class Dummy {
  public:
    static int n;
    Dummy () { n++; };

	//opposite of default constructor because we destroyed Dummy * c???
    ~Dummy () { n--; };
};

int Dummy::n=0;

int main () {
	//creation of object a caused default constructor to activate???
  Dummy a;
	//creation of object b sets static int n to 5 then increments n???
  Dummy b[5];

	//creating pointer to object c causes default constructor to 
	//increment n???
 	Dummy * c = new Dummy;
  cout << a.n << '\n';
	//destroying pointer to c returns value stored at &n to value prior to 
	//declaring pointer to c???
  delete c;

	//reflects the current value of static int n???
  cout << Dummy::n << '\n';
  return 0;
}
closed account (jvqpDjzh)
//opposite of default constructor because we destroyed Dummy * c???
It's not the opposite of default constructor: we can have more than one constructor (but just one default one)
This is named destructor and it's called when an object is destroyed.

//creation of object a caused default constructor to activate???
When you create an object, one of the constructors are called, in your case you have just the default constructor, so automatically it's called that, which is responsible for allocating memory for the variables of the internal status.


//creation of object b sets static int n to 5 then increments n???
Actually you are declaring an array of 5 objects of type Dummy.


//destroying pointer to c returns value stored at &n to value prior to
//declaring pointer to c???
Every time you call a desctructor your are deallocating the memory occupied by the object on which you call the destructor.
If you create an object dynamically with raw pointers, you have also to delete manually the memory occupied by that object, and you call delete, you are actually calling the destructor, which decreases the value of the static variable "n".


//reflects the current value of static int n???
Yes, but you can access to a static variable even using an object created.

EDIT: Think about static variables as class variables, and not a variable of each single object you possibly can create. That means that 1 variable can be shared by many objects.
Last edited on
Put a cout statement in your constructor and destructor to see how they are called:
1
2
    Dummy () { n++; cout << "constructor  n=" << n << endl; }
    ~Dummy () { n--; cout << "destructor  n=" << n << endl; }


Edit: removed unneeded semicolons.
Last edited on
Thanks!

I couldn't figure out why Dummy b[5] wasn't an array, but now I see how an array with 5 elements increments n 5 times.

Thanks also for the cout. I should have thought to try that.

Thanks also for the clarification on the constructor and destructor.
Registered users can post here. Sign in or register to post.