-2
int main() {
    doSomething(something);
}
    
string doSomething(Thing *x);

Here, doSomething is a function and Thing is a class. Now, I also have another inherited class called subThing, and I also want to doSomething to a pointer of subThing.

What do you call the concept of using pointers to inherited classes? I am asking this so that I can research more on this topic.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
12345bird
  • 1
  • 2

1 Answers1

3

Look up “Polymorphism”.

When subThing is derived from Thing, an instance of subThing is also an instance of Thing, so a subThing* pointer can be used anywhere a Thing* pointer can be used. Same thing with subThing& and Thing& references. Just watch out for “Object Slicing”. Polymorphic access to an object only works when accessing the object via a pointer or reference.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770