I recently had a situation where i had to use static_cast to cast a parent class to a child class, because i knew the object instance was that child class. I knew this based on an if condition.
Something like this:
parent* foo; child* bar; if(foo is instance of child class) bar = static_cast<child*>(foo) My question is: Why does static_cast always require pointers? This did not work when i tried it with non-pointer variables. An exception seems to be primitive data types.
Is this because every pointer can be cast as a void*? Is that how static_cast works?
Edit: I forgot to mention that it works with references. So the question, as it is currently framed, is wrong. Reframing the question to "Why does static_cast require pointers or references?"