M
Mike Jolley
Hello
First off, I'm a student so I'm pretty new to C++, and therefore I have
probably made a stupid mistake somewhere.
Anyway Ive been trying to fix this 5 hours straight now, so i need a
little assistance.
What I'm trying to do
I am using inheritance to make some bookings for a marina, which are:
Booking
|
__________________________________
| | |
narrowBoat motorBoat sailingBoat
These bookings are being stored in a vector in the bookingRecords
collection class:
private :
vector< booking > m_bookings;
I'm adding bookings like this:
void bookingRecords::addBooking(booking& v)
{
m_bookings.push_back( v ); //push to back of vector
}
Then iterating over them to run a method:
string bookingRecords::toString()
{
string retv("");
vector< booking >::iterator iter;
for (iter = m_bookings.begin(); iter !=m_bookings.end(); iter++)
{
retv+= iter->BtoString() + "\n";
}
return(retv);
}
The problem? When they get stored in this vector they lose their type
(e.g narrowBoat) and become a standard booking. I know this because when
I try to run a method (which is virtual in the booking class) it does
not run the correct method from one of the sub classes.
I did a bit of research and found a possible solution of using pointers
inside the vector instead of type booking, however I have not been able
to get this to work.
Any ideas on how to fix my problem? Am I totally in the wrong? I can
send my source on request if that helps.
Thanks for any assistance on this, it is much appreciated.
First off, I'm a student so I'm pretty new to C++, and therefore I have
probably made a stupid mistake somewhere.
Anyway Ive been trying to fix this 5 hours straight now, so i need a
little assistance.
What I'm trying to do
I am using inheritance to make some bookings for a marina, which are:
Booking
|
__________________________________
| | |
narrowBoat motorBoat sailingBoat
These bookings are being stored in a vector in the bookingRecords
collection class:
private :
vector< booking > m_bookings;
I'm adding bookings like this:
void bookingRecords::addBooking(booking& v)
{
m_bookings.push_back( v ); //push to back of vector
}
Then iterating over them to run a method:
string bookingRecords::toString()
{
string retv("");
vector< booking >::iterator iter;
for (iter = m_bookings.begin(); iter !=m_bookings.end(); iter++)
{
retv+= iter->BtoString() + "\n";
}
return(retv);
}
The problem? When they get stored in this vector they lose their type
(e.g narrowBoat) and become a standard booking. I know this because when
I try to run a method (which is virtual in the booking class) it does
not run the correct method from one of the sub classes.
I did a bit of research and found a possible solution of using pointers
inside the vector instead of type booking, however I have not been able
to get this to work.
Any ideas on how to fix my problem? Am I totally in the wrong? I can
send my source on request if that helps.
Thanks for any assistance on this, it is much appreciated.