概述如下:
void main()
{
Savings saObj1("3277",7000);
Savings saObj2("3279",5000);
Checking chObj1("8888");
Checking chObj2("398",10000);
list<Account*> we;
we.push_back (&saObj1);
we.push_back (&saObj2);
we.push_back (&chObj1);
we.push_back (&chObj1);
for( list< Account *>::iterator it = we.begin (); it != we.end(); ++it)
(*it)->display();//可以正常显示
ifstream in("account.txt");
Savings *x;
Checking *y;
while(s != "x 0")
{
getline(in, s);
{
if(s1 == "savings")
{
x = new Savings(s2,s3);//此处如果用Savings x(s2,s3)直接定义,则在循环外面输出时,不能够正常输出,为什么一定要用New 去定义对象。将他的内容放到we里不就可以不用管了吗?为什么非要用堆的对象才可以?
we.push_back (x);
}
else
{
y = new Checking(s2,s3);
we.push_back (y);
}
//在while外面
for( list< Account *>::iterator its = we.begin (); its != we.end(); ++its)
(*its)->display();
delete x;
delete y;
}
///////////
请高手不吝赐教,感激涕零