使用类型强制转换在单个向量中拥有不同的对象

Using type casts to have different objects in a single vector

本文关键字:拥有 对象 向量 单个 类型 转换      更新时间:2023-10-16

我试图使用一个paymentList向量,其中有现金,支票和信用对象(这是支付的派生类)在向量内。

我这样声明vector:

typedef std::vector<Payment*> ListOfPayments;

我像这样添加付款:

std::cout << "How would you like to pay?" << std::endl;
std::cout << "1. Cash"  <<std::endl;
std::cout << "2. Credit"<<std::endl;
std::cout << "3. Cheque"<<std::endl;
std::cin >> choice;
while(choice < 1 || choice > 3)
{
  std::cout<<"Please enter a correct number from 1 to 3"<<std::endl;
  std::cin >> choice;
}
if(choice == 1)
{
  paymentList->push_back(addCash(paymentId,orderId));
}
else if(choice == 2)
{
  paymentList->push_back(addCredit(paymentId,orderId));
}
else
{
  paymentList->push_back(addCheque(paymentId,orderId));
}

我现在想把这个矢量保存到一个文件中。我已经开始了一个保存功能,但我不确定从这里去哪里:

void savePayment(ListOfPayments *paymentList)
{
    int method;
    Cheque * pCheque = dynamic_cast<Cheque *>(paymentList->at(paymentList->size()-1));
    Cash * pCash = dynamic_cast<Cash *>(paymentList->at(paymentList->size()-1));
    Credit * pCredit = dynamic_cast<Credit *>(paymentList->at(paymentList->size()-1));
    std::ofstream* save = new std::ofstream(); // creates a pointer to a new ofstream
    save->open("Payments.txt"); //opens a text file called payments.
    if (!save->is_open())
    {
        std::cout<<"The file is not open.";
    }
    else
    {
        *save << paymentList->size() << "n";
        ListOfPayments::iterator iter = paymentList->begin(); 
        while(iter != paymentList->end()) //runs to end 
        {
            method = (*iter)->getMethod();
            *save << method << "n";
            if(method == 1)
            {
                pCash->saveCashPayments(save);
            }
            else if(method == 2)
            {
                pCredit->saveCreditPayments(save);
            }
            else
            {
                pCheque->saveChequePayments(save);
            }
            iter++;
        }
        save->close();
        delete save;
    }
}

如果我保存一种付款类型,它可以工作,但一旦我在列表中有两个或更多付款,我就会得到违规阅读位置错误。我猜这与类型转换错误或其他什么有关?如果我错了,这里是我的保存函数的一个例子,它基于方法变量运行。

void Cash::saveCashPayments(std::ofstream* save)
{
*save << this->cashTendered << "n";
*save << this->getId() << "n";
*save << this->getAmount() << "n";
*save << this->getOrderId() << "n";
*save << this->getMethod() << "n";
} 

任何帮助都将是感激的:)

这是完全错误的做法。

一个更好的方法是运行时多态性。在基类中声明一个名为Save的虚函数,并在每个派生类中定义它。

例如,如果Payment是基类,那么这样做:

class Payment
{
   public:
     virtual void Save(std::ostream & out) = 0;
};

然后在所有派生类中实现Save

class Cheque : public Payment
{
   public:
     virtual void Save(std::ostream & out) 
     {
            //implement it
     }
};
class Cash : public Payment
{
   public:
     virtual void Save(std::ostream & out) 
     {
            //implement it
     }
};
class Credit : public Payment
{
   public:
     virtual void Save(std::ostream & out) 
     {
            //implement it
     }
};

然后使用Payment*类型的指针调用Save

void savePayment(ListOfPayments & payments)
{
    std::ofstream file("Payments.txt");
    for(ListOfPayments::iterator it = payments.begin(); it != payments.end(); ++it)
    {
         it->Save(file);
    }
}

不需要通过指针传递payment;也不要使用new std::ofstream .

阅读c++中的运行时多态性