【求解】未定义的析构函数
abc说明:定义 del 函数的时候,abc 的析构函数未定义,因此不会调用。看如下代码,试解释器运行结果
#include <stdio.h>
class abc;
void del(abc *pobj)
{
delete pobj;
}
class abc{
public:
abc()
{
printf("abc\r\n");
}
~abc()
{
printf("~abc\r\n");
}
};
int main(int argc, char *argv[])
{
abc *pobj = new abc;
del(pobj);
return 0;
}
编译出现警告
C:\Users\Ares\Desktop>g++ test.cpp
test.cpp: In function `void del(abc*)':
test.cpp:5: warning: possible problem detected in invocation of delete operator:
test.cpp:4: warning: `pobj' has incomplete type
test.cpp:2: warning: forward declaration of `struct abc'
test.cpp:5: note: neither the destructor nor the class-specific operator delete
will be called, even if they are declared when the class is defined.
test.cpp:27:2: warning: no newline at end of file
abc
说明:定义 del 函数的时候,abc 的析构函数未定义,因此不会调用。感觉有说服力么?