什么样的指针类型可以互相转换?
一定是 基类和 派生类 的指针可以互相转换么? 还有其他的情况么?
[解决办法]
#include <string>#include <iostream>using namespace std;int main(){ string *a = new string("abcdefg....sss"); cout << a << endl; cout << *a << endl; int * b = (int*)a; int * c = (int*)(sizeof (int) + (unsigned)a); delete b; cout << "delete b OK" << endl; delete c; cout << "delete c OK" << endl; return 0;};
[解决办法]
如果没有必要,千万不要转换指针后delete,因为这样有可能会导致析构函数没有被调用....
当然,如果你的只是基本类型,就没有这个顾虑了....