首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

什么样的指针部类可以互相转换

2012-09-25 
什么样的指针类型可以互相转换?一定是 基类和 派生类 的指针可以互相转换么? 还有其他的情况么?[解决办法]

什么样的指针类型可以互相转换?
一定是 基类和 派生类 的指针可以互相转换么? 还有其他的情况么?

[解决办法]

探讨

C/C++ code

int main()
{
string *a = new string("abcdefg....sss");
cout << a << endl;
cout << *a << endl;
int * b = (int*)a;
int * c = new int(38);
cout <<"指针a大小:" << sizeo……

[解决办法]
C/C++ code
#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,因为这样有可能会导致析构函数没有被调用....

当然,如果你的只是基本类型,就没有这个顾虑了....

热点排行