#include<iostream.h>
#include<string.h>
struct item{
char*name;
long telnum;
};
class tel_array{
private:
item*table;
int max,items;
public:
tel_array(int nm=0){
max=nm;
items=0;
table=new item[max];
}
long&operator[](char*nm){
item*pi;
for(pi=table;pi<table+items;pi++)
if(strcmp(pi->name,nm)==0)
return pi->telnum;
pi=table+items++;
pi->name=new char[strlen(nm)+1];
strcpy(pi->name,nm);
pi->telnum;
return pi->telnum;
}
void printall(){
for(int i=0;i<items;i++)
cout<<table[i].name<<"."<<table[i].telnum<<endl;
}
};
void main(){
tel_array telnum_table(10);
telnum_table["zhao"]=123456;
telnum_table["Qian"]=123590;
cout<<"zhao"<<telnum_table["zhao"]<<endl;//cout<<"ok"<<endl;
telnum_table.printall();
}
帮我解析一下加粗部分的代码,为什么是table=new item[max]?
pi是item的对象,怎么和int型的items相加呀?