From this question, I implement like this
Utils.h
:
template<class T>
void SafeDeleteWithoutClass(T* &pVal) {
std::cout << __FUNCTION__ << std::endl;
delete pVal;
pVal = nullptr;
}
template<class T>
void SafeDeleteArrayWithoutClass(T* &pVal) {
std::cout << __FUNCTION__ << std::endl;
delete[] pVal;
pVal = nullptr;
}
main.cpp
#include "Utils.h"
struct Foo {
Foo() {std::cout << __FUNCTION__ << std::endl;}
~Foo() {std::cout << __FUNCTION__ << std::endl;}
}
int main()
{
Foo *pObj = new Foo();
SafeDeleteWithoutClass<Foo>(pObj2);
return 0;
}
It works well. But some guys in my team said it is not really better than macro. I don't know whether any example to prove this way is better than macro. Could you give me a hint?