Using type casts to have different objects in a single vector
<p>I am trying to use a paymentList vector which has Cash, Cheque and Credit objects (which are derived classes of Payment) inside of the vector.</p>

<p>I declare the vector like this:</p>

<pre><code>typedef std::vector&lt;Payment*&gt; ListOfPayments;
</code></pre>

<p>I add payments like this:</p>

<pre><code>std::cout &lt;&lt; "How would you like to pay?" &lt;&lt; std::endl;
std::cout &lt;&lt; "1. Cash"  &lt;&lt;std::endl;
std::cout &lt;&lt; "2. Credit"&lt;&lt;std::endl;
std::cout &lt;&lt; "3. Cheque"&lt;&lt;std::endl;
std::cin &gt;&gt; choice;

while(choice &lt; 1 || choice &gt; 3)
{
  std::cout&lt;&lt;"Please enter a correct number from 1 to 3"&lt;&lt;std::endl;
  std::cin &gt;&gt; choice;
}
if(choice == 1)
{
  paymentList-&gt;push_back(addCash(paymentId,orderId));
}
else if(choice == 2)
{
  paymentList-&gt;push_back(addCredit(paymentId,orderId));
}
else
{
  paymentList-&gt;push_back(addCheque(paymentId,orderId));
}
</code></pre>

<p>I now want to save this vector to a file. I have started a save function but I'm unsure where to go from here:</p>

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

<p>It works if I save one type of payment, but as soon as I have two or more payments in the list I get a violation reading location error. I'm guessing it has to do with the type casts being wrong or something? In case I'm wrong here is an example of my save function that runs based on the method variable.</p>

<pre><code>void Cash::saveCashPayments(std::ofstream* save)
{
*save &lt;&lt; this-&gt;cashTendered &lt;&lt; "\n";
*save &lt;&lt; this-&gt;getId() &lt;&lt; "\n";
*save &lt;&lt; this-&gt;getAmount() &lt;&lt; "\n";
*save &lt;&lt; this-&gt;getOrderId() &lt;&lt; "\n";
*save &lt;&lt; this-&gt;getMethod() &lt;&lt; "\n";
} 
</code></pre>

<p>Any help would be appreciated :) </p>
