I have a binary file that I have written base objects. I want to read those base objects into a vector of objects that I have derived from base.
Can it be done and how.
Code:#include <vector> class CBase { public: CBase( ) : m_val1(0.0f), m_val2(0.0f) { } virtual ~CBase( ) { } float m_val1; float m_val2; }; class CDerived : public CBase { public: CDerived( ) : m_val3(0.0f) { } CDerived(const CBase& base ) : CBase(base), m_val3(0.0f) { } virtual ~CDerived( ) { } float m_val3; }; int main(int argc, char* argv[]) { // how to: read base objects into vector of derived objects std::vector<CDerived> myVector; myVector.resize(5); // let's say my binary file has 5 base objects in it FILE *fp = fopen("somefile", "rb"); // can I write this line of code to write the base objects into vector fread(&myVector[0], 5, sizeof(CBase), fp); fclose( fp ); return 0; }




Reply With Quote