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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
class A
{
static int _total; //indique le nombre total d'instance qui partage _entier
bool _shared; //indique si il y a été crée par le constructeur par recopie
public:
A()
{
_shared = false;
_total++;
}
A(const A & a)
{
_shared = true;
}
~A()
{
if(!_shared)
_total--;
}
int getTotal()
{
return _total;
}
};
int A::_total = 0;
int main()
{
A *a = new A;
std::cout<<"Total : "<<a->getTotal()<<std::endl;
A * b = new A;
std::cout<<"Total : "<<a->getTotal()<<std::endl;
delete a;
std::cout<<"Total : "<<b->getTotal()<<std::endl;
A * c = new A(*b);
std::cout<<"Total : "<<c->getTotal()<<std::endl;
delete c;
std::cout<<"Total : "<<b->getTotal()<<std::endl;
delete b;
return 0;
} |
Partager