1

I have a parent class called List and a child class called SortedList. In List, I overload += so that it calls the member function Insert. That is:

void List::operator+=(std::string newEntry) {    
    this->Insert(newEntry);  
}  

Both List and SortedList have an Insert function. Ideally, if the instance of List that I'm using is also a SortedList, SortedList's Insert function would be called instead of List's, but that's not what is happening. I could redefine the above function in SortedList and every subsequent child class of List that I will create, but that seems redundant. Is there any way to modify the above function to get the desired behavior?

  • 1
    Thank you. I hadn't heard of virtual methods before. All I had to do was add `virtual` in front of `List`'s `Insert` function declaration. – Jonathan Hinson Oct 18 '14 at 06:04

0 Answers0