{ nptd++ ;
return ::new char[sz] ; // appelle new prédéfini
}
void operator delete (void * dp)
{ nptd-- ;
::delete (dp) ; // appelle delete prédéfini
}
static int npt_tot ()
{ return npt ;
}
static int npt_dyn ()
{ return nptd ;
}
} ;
int point::npt = 0 ; // initialisation des membres statiques de point
int point::nptd = 0 ;
Notez que, dans la surdéfinition de new et delete, nous avons fait appel aux opérateurs
prédéfinis (par emploi de ::) pour ce qui concerne la gestion de la mémoire.
Voici un exemple de programme utilisant notre classe point, accompagné du résultat
fourni par son exécution :
#include "point.h"
#include
using namespace std ;
main()
{
point * ad1, * ad2 ;
point a(3,5) ;
cout << "A : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
ad1 = new point (1,3) ;
point b ;
cout << "B : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
ad2 = new point (2,0) ;
delete ad1 ;
cout << "C : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
point c(2) ;
delete ad2 ;
cout << "D : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
}
A : 1 0
B : 3 1
C : 3 1
D : 3 0
225
return ::new char[sz] ; // appelle new prédéfini
}
void operator delete (void * dp)
{ nptd-- ;
::delete (dp) ; // appelle delete prédéfini
}
static int npt_tot ()
{ return npt ;
}
static int npt_dyn ()
{ return nptd ;
}
} ;
int point::npt = 0 ; // initialisation des membres statiques de point
int point::nptd = 0 ;
Notez que, dans la surdéfinition de new et delete, nous avons fait appel aux opérateurs
prédéfinis (par emploi de ::) pour ce qui concerne la gestion de la mémoire.
Voici un exemple de programme utilisant notre classe point, accompagné du résultat
fourni par son exécution :
#include "point.h"
#include
using namespace std ;
main()
{
point * ad1, * ad2 ;
point a(3,5) ;
cout << "A : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
ad1 = new point (1,3) ;
point b ;
cout << "B : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
ad2 = new point (2,0) ;
delete ad1 ;
cout << "C : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
point c(2) ;
delete ad2 ;
cout << "D : " << point::npt_tot () << " " << point::npt_dyn () << "\n" ;
}
A : 1 0
B : 3 1
C : 3 1
D : 3 0
225
