C++指標程式設計你要小心的陷阱——如何優雅的釋放指標void*
阿新 • • 發佈:2019-02-16
$ g++ -std=c++11 safe_delete_demo.cpp
safe_delete_demo.cpp: In instantiation of 'void safe_delete(T*&) [with T = void]':
safe_delete_demo.cpp:59:16: required from here
safe_delete_demo.cpp:9:9: warning: deleting 'void*' is undefined [enabled by default]
delete target;
$ ./a.out
safe_delete pointer of type AChild
child class AChild's destructor, name: jacky, school: Shenzhen University
base class A's destructor, name: jacky
safe_delete pointer of type void *
safe_delete_void_ptr pointer of type void * ==> A *
child class AChild's destructor, name: Heral, school: Renmin University of China
base class A's destructor, name: Heral
safe_delete_void_ptr pointer of type void * ==> AChild *
child class AChild's destructor, name: pevly, school: Southeast University
base class A's destructor, name: pevly
safe_delete_demo.cpp: In instantiation of 'void safe_delete(T*&) [with T = void]':
safe_delete_demo.cpp:59:16: required from here
safe_delete_demo.cpp:9:9: warning: deleting 'void*' is undefined [enabled by default]
delete target;
$ ./a.out
safe_delete pointer of type AChild
child class AChild's destructor, name: jacky, school: Shenzhen University
base class A's destructor, name: jacky
safe_delete pointer of type void *
safe_delete_void_ptr pointer of type void * ==> A *
child class AChild's destructor, name: Heral, school: Renmin University of China
base class A's destructor, name: Heral
safe_delete_void_ptr pointer of type void * ==> AChild *
child class AChild's destructor, name: pevly, school: Southeast University
base class A's destructor, name: pevly