![]() |
#2
lyj232011-05-22 17:22
|

……
class Cat{
public:
Cat(){cout<<"Cat()"<<endl;}
~Cat(){cout<<"~Cat()"<<endl;}
};
class Dog{
public:
void* operator new(size_t sz){ //这里为什么是void* 类型?下面delete的为什么没有*
cout<<"allocating a Dog"<<endl;throw 47;}
void operator delete(void *p){
cout<<"deallocating a Dog"<<endl;
::operator delete(p);}
};
class Use{
Dog* op;
Cat* bp;
pubilc:
Use(int count=1){
cout<<"Use()"<<endl;
bp=new cat[count];
op=new Dog;} //这里会不会调用重载的函数,如果调用了,那Dog的空间不是不会被创建吗!(我认为不会调用吧)
~Use(){
cout<<"~Use()"<<endl;
delete[] bp;//这里应该不调用重载吧。有个[]
delete op;}//这里调用重载吗?
};
int main(){
try{
Use ur(3);
}catch(int){
cout<<"error"<<endl;}
}//---------------------------
请解决上述问题,其他的代码仅供参考 class Cat{
public:
Cat(){cout<<"Cat()"<<endl;}
~Cat(){cout<<"~Cat()"<<endl;}
};
class Dog{
public:
void* operator new(size_t sz){ //这里为什么是void* 类型?下面delete的为什么没有*
cout<<"allocating a Dog"<<endl;throw 47;}
void operator delete(void *p){
cout<<"deallocating a Dog"<<endl;
::operator delete(p);}
};
class Use{
Dog* op;
Cat* bp;
pubilc:
Use(int count=1){
cout<<"Use()"<<endl;
bp=new cat[count];
op=new Dog;} //这里会不会调用重载的函数,如果调用了,那Dog的空间不是不会被创建吗!(我认为不会调用吧)
~Use(){
cout<<"~Use()"<<endl;
delete[] bp;//这里应该不调用重载吧。有个[]
delete op;}//这里调用重载吗?
};
int main(){
try{
Use ur(3);
}catch(int){
cout<<"error"<<endl;}
}//---------------------------