Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine type of child class with baseclass pointer

Tags:

c++

Is there a way to determine the type of a child class with a baseclass pointer?

I have declared a pointer in a class constructor like this in .h file

baseclass *screen;

in constructor

screen = new childclass();

Lets say baseclass has 5 different child classes and in my program I switch my pointer around to point towards various child objects, how can I determine the type of the object that screen is currently pointing towards?

like image 955
Jacco Avatar asked Feb 14 '23 09:02

Jacco


1 Answers

Don't.

Use virtual dispatch to achieve different behaviours for different derived types.

like image 86
Lightness Races in Orbit Avatar answered Feb 16 '23 03:02

Lightness Races in Orbit