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.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular