This article describes the CNoTrackObject class in C + + and the new delete operator overload, shared for everyone to reference. Specifically as follows:
Header information:
Copy Code code as follows:
Class cnotrackobject{
Public://Here is the fault, no add public default is a private variable for the class, and Mythreaddata cannot access the member variable after inheriting this class
void* operator new (size_t nsize);
void operator Delete (void*);
Virtual ~cnotrackobject () {}
};
The implementation method is as follows:
Copy Code code as follows:
void* cnotrackobject::operator New (size_t nsize)
{
Request a piece of memory
void* p =:: GlobalAlloc (Gptr, nsize);
return p;
}
void Cnotrackobject::operator Delete (void* p)
{
if (P!= NULL)
{
:: GlobalFree (P);
}
}
The use process is as follows:
Copy Code code as follows:
struct Mythreaddata:public cnotrackobject
{
mythreaddata* Pnext;
int nshortdata;
};
for (int i=0;i<10;i++)
{
<span style= "color: #ff0000;" >pdata = new Mythreaddata;</span>
Pdata->nshortdata = i;
List. AddHead (PData);
}
Traversing the list, freeing the space occupied by the Mythreaddata object
PData = (mythreaddata*) list. GetHead ();
while (PData!= NULL)
{
mythreaddata* Pnextdata = pdata->pnext;
printf ("The value is%d\n", pdata->nshortdata);
<span style= "color: #ff0000;" >delete pdata;</span>
PData = Pnextdata;
}
I hope this article will help you with the C + + program design.