-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.

5
  • 2
    Are you talking about "polymorphism"? Commented Jan 10, 2021 at 5:41
  • Are you asking "How does virtual function dispatch work?" Commented Jan 10, 2021 at 5:41
  • polymorphism is the idea that pointer's type may not exactly match the type of the thing it is pointing at. Commented Jan 10, 2021 at 5:42
  • Another keyword to check is "upcasting". Commented Jan 10, 2021 at 5:42
  • Yes, thanks! It is polymorphism. Commented Jan 10, 2021 at 13:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.