In my C++ code, I have a vector of pointers to a Light object. P_Light is a subclass of Light, and has a field position. For each pointer pointing to a Light that is actually a P_Light, I need to do something with the position field. I did some searching and it seems like I can implement a virtual method, but I don't need the method in Light since other types of Light don't have a position. I also thought about casting but I'm not sure how to make that work.
std::vector<Vector> light_dirs;
for(int i=0; i<lights.size; i++){
Light *l = lights[i];
//cast here?
}
EDIT:: Saw in a different post that maybe using qobject_cast would be a good idea. Does this look better?
std::vector<Vector> light_dirs;
for(int i=0; i<lights.size; i++){
Light *l = lights[i];
P_Light* pl = qobject_cast<P_Light*>(l);
if(pl != nullptr) //dostuff;
}
This should work.
P_Light* p_light = dynamic_cast<P_Light*>(l);
Check RTTI and dynamic cast on https://en.wikipedia.org/wiki/Run-time_type_information