C++ Builder 参考手册 ➙ System ➙ TObject ➙ NewInstance
头文件:#include <systobj.h>
命名空间:System
函数原型:
__classmethod virtual __unsafe TObject* __fastcall NewInstance();
System::TObject::NewInstance 是 System::TObject 的成员函数,给实例分配内存,并且返回新的实例的地址。
- 构造函数内部调用了 NewInstance,不要直接调用这个函数;
- 初始化成员需要写在构造函数里面;
- 如果重载了 NewInstance 就必须重载 FreeInstance 与之对应;
- NewInstance 通过 InstanceSize 获取分配内存的字节数;
- 重载 NewInstance 的时候,在 NewInstance 的最后一句要调用 InitInstance;
- 和 NewInstance 一样,FreeInstance 也是通过 InstanceSize 获取释放内存的字节数。
- 不要重载 InitInstance,因为它不是 virtual 函数,初始化成员要写在构造函数里面。
参考:
- System::TObject::CleanupInstance
- System::TObject::InitInstance
- System::TObject::InstanceSize
- System::TObject::FreeInstance
- System::TObject
- VCL 类继承关系
C++ Builder 参考手册 ➙ System ➙ TObject ➙ NewInstance