throw 的用法 helpme
#include <iostream>
#include <cstring>
#include <cstddef>
#include <new>
const int maxname = 5;
class Names
{
char name[25];
static char Names::pool[];
static bool Names::inuse[maxname];
public:
Names(char* s)
{
strcpy(name, s);
};
void* operator new(size_t) throw(bad_alloc);
void operator delete(void*) throw();
void display() const
{
std::cout<<name<<std::endl;
}
};
char Names::pool[maxname * sizeof(Names)];
bool Names::inuse[maxname];
void* Names::operator new(size_t size) throw(bad_alloc)
{
for (int p=0; p<maxname; p++)
{
if ( !inuse[p] )
{
inuse[p] = true;
return pool + p*sizeof(Names);
}
}
throw bad_alloc();
}
void Names::operator delete(void* p) throw()
{
if ( p != 0 )
inuse[((char*)p - pool)/sizeof(Names)] = false;
}
int main()
{
Name* nm[maxnames];
int i;
for( i=0; i<maxnames; i++ )
{
std::cout<<std::endl<<"Enter name #"<<i+1<<" :";
char name[25];
std::cin>>name;
nm[i] = new Names(name);
}
for ( i=0; i<maxnames; i++ )
{
nm[i]->display();
delete nm[i];
}
return 0;
}
各位大虾,帮我调试下程序。