使用类型强制转换在单个向量中拥有不同的对象
Using type casts to have different objects in a single vector
我试图使用一个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++中的运行时多态性
相关文章:
- 是否可以获取成员函数模板参数的拥有对象?
- 拥有或在对象之间共享资源
- googlemock-如何模拟被测试类所拥有的对象
- C++ shared_ptr调用拥有对象的解构函数
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 使用 lambda/std::function 删除拥有该函数的对象
- QT:QT无法将事件发送到另一个线程拥有的对象 - 原因
- 当 std::unique_ptr 在手动删除其拥有的对象后超出范围时,它如何工作?
- 谁拥有QQMlinCubator返回的对象
- 带有 std::map 的 c++ 结构是拥有动态数据对象的好设计吗?
- 是否可以使用对shared_ptr拥有的对象的引用
- 将原始指针向量清除给由独特指针拥有的对象
- 如何将 QEvent 传递给接收者拥有的对象?
- 如何正确地使一个对象拥有另一个多态对象
- C++拥有持久对象的最佳方式
- 将类的实例传递给另一个构造函数,该构造函数将其对象添加到所传递实例所拥有的列表中
- C++正在更改子类中拥有的对象类型
- 如何将此的shared_ptr返回给拥有它的对象
- 类同时拥有和共享对象
- 如何根据模板类型拥有对象向量