什么时候应该动态建立对象
一个输入操作符重载
类:
class CheckoutRecord
{
public:
.....
private:
double book_id;
string title;
Date date_borrowed;
Date date_due;
pair<string,string> borrower;
vector<pair<string,string>* > wait_list;
};
重载操作符:
istream& operator>>(istream& in, CheckoutRecord& obj)
{
in>>obj.book)id>>obj.title>>obj.date_borrowed>>obj.date_due>>obj.borrower.first>>obj.borrower.second;
if(!in)
{
obj=CheckoutRecord();
return in;
}
//给vector容器输入成员的时候,需要先将vector中的成员都删除。然后再输入。由于vector中元素是指针所以需要
动态建立相应的pair对象。
obj.wait_list.clear();//删除vector中的所有元素
while(in)
{
pair<string,string>* p=new pair<string,string>
in>>p->first>>p->second;
if(!in)
{
return in;
}
obj.wait_list.push_back(p);
}
return in
}
//我想问问大家,什么时候应该建立动态对象,什么时候需要?