Wednesday, May 18, 2011

3.2 - Statikus tagok

Az alabbi pelda szemlelteti a statikus tagok deklaraciojat, definiciojat es hasznalatat.

#include 
using namespace std;

class InstanceCounter{
 private:
  static int counter; //Deklaracio
  int i;
 public:
  InstanceCounter(int _i=0) : i(_i){counter++;}
  ~InstanceCounter(){counter--;}
  static int getCounter(){return counter;}
};

int InstanceCounter::counter=0; //Definico

int main(){
 cout<<"Peldanyszam" << InstanceCounter::getCounter() << endl;
 InstanceCounter o1 o2 o3;
 cout<<"Peldanyszam" << InstanceCounter::getCounter() << endl;
 InstanceCounter * p = new InstanceCounter(10);
 cout<<"Peldanyszam" << InstanceCounter::getCounter() << endl;
 delete p;
 cout<<"Peldanyszam" << InstanceCounter::getCounter() << endl;
 return 0;
}

No comments:

Post a Comment