Inserting Different Types of Objects in STL Lists

Inserting Different Types of Objects in STL Lists

I need a container of different objects. I have made all derived classes of the same base class and pushed them onto the container. The problem then arises in getting them off the container,i.e., base class synObject, and synPin and synPin derived:

class synObject { public : synObject(); string GetClass(); string className;};synObject::synObject(){ className = "synObject";}string synObject::GetClass(){ return className;}class synPin : public synObject { string pin; public : synPin(); void   SetPin(string Pin); string GetPin(); private:};synPin::synPin(){ className = "synPin";}void synPin::SetPin(string Pin){ pin = Pin;}string synPin::GetPin(){ return pin;}class synCell : public synObject { string cell; public : synCell(); void   SetCell(string Cell); string GetCell(); private:};synCell::synCell(){ className = "synCell";}void synCell::SetCell(string Cell){ cell = Cell;}string synCell::GetCell(){ return cell;}

I then use the classes, and push to a list of vector pointers.

 synObject * pMyObject; pMyObject = new synObject; synPin * pMyPin; pMyPin = new synPin; (*pMyPin).SetPin("myPin"); synCell * pMyCell; pMyCell = new synCell; (*pMyCell).SetCell("myCell");  vector MyVector; vector::iterator ThisVector; MyVector.empty(); MyVector.push_back(pMyObject); MyVector.push_back(pMyPin); MyVector.push_back(pMyCell);

When I come to iteregate the vector though, I can only access the common(base) methods: not those derived from the base class.

 for ( ThisVector  = MyVector.begin();         ThisVector != MyVector.end();        ThisVector++     ) {  cout << (**ThisVector).GetClass() << endl ;  if ( (**ThisVector).GetClass().compare("synClass") == 0) {   cout << (**ThisVector).GetCell() << endl ;  }  if ( (**ThisVector).GetClass().compare("synPin") == 0) {   cout << (**ThisVector).GetPin() << endl ;  } }

To achieve dynamic binding, you need to use a reference or pointer to a base class. If you're using a pointer to base, don't use the dot notation to call a member function, but rather, use the -> after the pointer's name. In other words, instead of:




More importantly, instead of accessing the vector's elements through an iterator, use the overloaded [] operator:

vector < synObject * >::iterator ThisVector= MyVector.begin();for (int i =0; ThisVector!=MyVector.end(); ++i){  cout << MyVector[i]->GetClass();} 

I have some comments about your code, some of which are stylistic; others are more crucial.

First, note also that you classes don't declare any virtual members, not even a virtual destructor, which is likely to cause undefined behavior. This leads me to question the usefulness and soundness of using inheritance in this case. Additionally, you shouldn't return string objects by value from a member function. Instead, return a reference to a const data member. Finally, make sure that you properly destroy the allocated objects using delete.

See also  Digital Signage Solutions for Agile Software Development Teams

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist