I. Review of History
In C + +, objects can be created either on the stack or in the heap. Such as:
class CTestClass
{
public:
CTestClass()
{
printf(“Create”);
}
void DoPrint() {}
~CTestClass()
{
printf(“destroy”);
}
}
The following code creates a stack object
CTestClass test;
test.DoPrint();
Stack object lifecycle is managed by the background. When the method ends, the Stack object pops out of the stack, and the compiler automatically destroys the objects that are ejected from the stack.
The following code creates a heap object
CTestClass* test = new CTestClass();
test->DoPrint();
Heap objects are stored in the heap, the heap object lifecycle is not managed by the background, and the programmer must manually release the heap object, or it will cause a memory leak:
delete test;
test = NULL;
Pascal language supports object-oriented from OOP Pascal, i.e. OOP Pascal supports creating objects. As with OOP Pascal and C + +, you can also create stack objects and heap objects, respectively: