用C++编写一个模板 Pair < T , U > ,用于存放一个由两种任意类型对象组 ...
发布网友
发布时间:2024-10-23 20:41
我来回答
共1个回答
热心网友
时间:5小时前
你的程序存在一个深拷贝的问题,必须要添加上对应的拷贝构造函数。
另外,因为你要求使用静态变量count来存储创建的对象的个数,而在代码中很多地方,你要求使用值传递而不是引用来作为函数参数,所以不可避免会产生很多的临时变量(多次调用构造及析构函数),count的值会反复变化。
以下是程序代码,请参考,如果有疑问或我的理解有偏差,欢迎追问。
#include <iostream>
#include <cstring>
using namespace std;
template<class T, class U> class Pair
{
private:
T m_first;
U m_second;
public:
Pair()
{
}
Pair(T t, U u):m_first(t), m_second(u)
{
}
T get_first()
{
return m_first;
}
U get_second()
{
return m_second;
}
};
class Staff
{
#define STAFF_START_ID 100000001
friend ostream& operator<< (ostream& out , Staff staff );
private:
long m_id;//职员 id 号,不同职员的唯一区别标识;
char* m_name;//职员姓名,指向一个动态建立的姓名字符串
char* m_sex;//职员性别,指向一个动态建立的性别描述串;
int m_age;//职员年龄。
static long count;
public:
//构造职员对象,并为属性赋值。
Staff(long id , char* name , char* sex , int age )
{
if(NULL!=name)
{
m_name=new char[strlen(name)];
strcpy(m_name, name);
}
else
{
m_name=NULL;
}
if(NULL!=sex)
{
m_sex=new char[strlen(sex)];
strcpy(m_sex, sex);
}
else
{
m_sex=NULL;
}
m_id=id;
count++;
m_age=age;
}
Staff(const Staff& src)
{
*this = src;
count++;
}
Staff& operator = (const Staff& src)
{
if(NULL!=src.m_name)
{
m_name=new char[strlen(src.m_name)];
strcpy(m_name, src.m_name);
}
else
{
m_name=NULL;
}
if(NULL!=src.m_sex)
{
m_sex=new char[strlen(src.m_sex)];
strcpy(m_sex, src.m_sex);
}
else
{
m_sex=NULL;
}
m_id=src.m_id;
m_age = src.m_age;
return *this;
}
//注意对 name 和 sex 所指向的字符串内存空间进行释放。
~Staff()
{
if (NULL!=m_name)
{
delete []m_name;
m_name=NULL;
}
if (NULL!=m_sex)
{
delete []m_sex;
m_sex=NULL;
}
count--;
}
long get_id () const//获取职员的 id 号。
{
return m_id;
}
const char *get_name() const
{
return m_name;
}
const char *get_set() const
{
return m_sex;
}
int get_age() const
{
return m_age;
}
static long get_Staff_count()
{
return count;
}
};
ostream& operator<< (ostream& out , Staff staff )
{
out<<"Id:"<<staff.m_id<<"\t";
out<<"Name:"<<staff.m_name<<"\t";
out<<"Sex:"<<staff.m_sex<<"\t";
out<<"Age:"<<staff.m_age;
return out;
}
long Staff::count=STAFF_START_ID;
typedef Pair<long, Staff> Map;
#define ARRAY_COUNT 10
int main()
{
Map *staffmap[ARRAY_COUNT];
int idx=0;
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Tom", "male", 10));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Lee", "male", 11));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Jacky", "male", 12));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Bryan", "male", 9));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Jeccia", "female", 8));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Gary", "male", 7));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Hugo", "male", 14));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Susan", "female", 15));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Selina", "female", 16));
staffmap[idx++] = new Map(Staff::get_Staff_count(), Staff(Staff::get_Staff_count(), "Shriley", "female", 17));
#if 0//debug info
for (int i=0; i<ARRAY_COUNT; i++)
cout<<staffmap[i]->get_second()<<endl;
#endif
long input=0;
do
{
cout<<"Please input the id of the staff(enter -1 to exit):";
cin>>input;
if (-1 == input)
break;
for (idx=0; idx<ARRAY_COUNT; idx++)
{
if (staffmap[idx]->get_first()==input)
{
cout<<staffmap[idx]->get_second()<<endl;
break;
}
}
if (idx==ARRAY_COUNT)
cout<<"Staff with id:"<<input<<" not found!"<<endl;
cout<<endl;
}
while(1);
for (idx=0; idx<ARRAY_COUNT; idx++)
{
delete staffmap[idx];
}
return 0;
}