Inserting Different Types of Objects in STL Lists

Question:
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 ;  } }

Answer:
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:

 (*pMyCell).SetCell("myCell");

Write:

 pMyCell->SetCell("myCell");

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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may