I am having some issues with implementing a solution that deals with object creation on the heap in a loop and stores this in an array.
I have a transaction class that represents a share transaction. It includes custom date and time classes as well as some double values to store numeric values.
I read these using my TransactionIO class from a CSV file that contains around 100 transaction records.
My algorithm is as follows:
While EOF not reached
Read Transaction data
Create Transaction object on heap
Return pointer to newly created Transaction object
Store 'Pointed-to-Transaction' in custom Vector
EndWhile
Here is the code for the TransactionIO function ReadTransaction:
// Reads in a transaction and returns it
Transaction* TransactionIO::ReadTransaction(ifstream& is)
{
// Assuming file structure is a CSV file
// With structure given as:
// Date/Time, Price, Volume, Value, Condition
Date date;
Time time;
double price, volume, value;
string condition;
string dateString, timeString;
//cout << "\nBegin Read..." << endl;
getline(is, dateString, ','); // read date/time string and then parse
ReadNextDoubleField(price, is);
ReadNextDoubleField(volume, is);
ReadNextDoubleField(value, is);
getline(is, condition);
// split the date and time fields and parse them
timeString = dateString.substr(dateString.find_first_of(" ") + 1);
dateString.erase(dateString.find_first_of(" ")); // remove the time string - only need date string here
date = ParseDate(dateString); // Will change later to use pass by reference
time = ParseTime(timeString); // Will change later to use pass by reference
// construct and return transaction that was read
Transaction* transaction = new Transaction(date, time, price, volume, value, condition);
return transaction
}
I have not created my main class yet, as I was working on my data classes. So what would be the correct way to use this function?
I am planning on doing this:
In the loop in main:
While(//file IO condition here...)
{
p_transaction = TransactionIO::ReadTransaction(is);
myCustomVector.Add(*transaction);
}
Is this the right way to do it? My custom vector's Add method expects a const T& reference to add the given object.
Also, will calling delete[] on my custom vector's internal array delete the objects being stored in it?
I feel my code is very inefficient and my lecturer has warned me to not construct objects in loops. I should return objects by reference.
But in this case If i tried that wouldn't this be invalid?
void TransactionIO::ReadTransaction(Transaction& transaction, ifstream& is)
{
// Do all the reading and processing as given above....
Transaction t1(date, time, price, volume, value, condition);
transaction = t1; // creating object AND call assignment op - not efficient?
}
In the above line, once this function completes, t1 will go out of scope and the object will destroyed. So what will my reference be pointing to?
Which is why I decided on the pointer solution, but I feel that my program will have a huge memory leak...
Any help and explanation would be highly appreciated... I like to know not just the 'how' but also the 'why' of your answer if possible.
And lastly, no. I cannot use STL containers. I have to use my own vector class (I have tested it out and it works well.)