No, no and another time no.
The line that simply doesn't pass muster is this:
function(func);
This requires a downcast, i.e. - a cast from a parent class to its subclass. A downcast will not happen implicitly, which means the line will not compile (but had you provided compiling code, you would already know that). The following would compile:
function((classF*)func);
but you shouldn't use it. A downcast's result is undefined unless that reference originally belonged to that type. So, this is okay:
classF *f = new classF(); func = (classA*) f; function((classF*)func);
But this is not:
classB *b = new classB(); func = (classA*) b; function((classF*)func);
Edited to add
If the downcast is performed using the dedicated C++ cast operator dynamic_cast, then the code, at run time, makes sure that the down cast is correct. So:
classF *f = new classF(); func = (classA*) f; classF *newf = dynamic_cast<classF *>(func); // Okay classB *b = new classB(); func = (classA*) b; newf = dynamic_cast<classF *>(func); // newf is now NULL
This allows run time checking that you have not made a mistake.
Personally, I like never relying on this for actual design, only for verification. In such a case, it is better to dynamic cast references rather than pointer. Dynamic casting an incorrect pointer results in NULL. Dynamic casting an incorrect reference throws an exception:
classF *f = new classF(); func = (classA*) f; classF *newf = &dynamic_cast<classF &>(*func); // Okay classB *b = new classB(); func = (classA*) b; newf = &dynamic_cast<classF &>(*func); // Throws an exception
static_castin this case.classFis required, you need to serveclassF. IfclassAprovides everything that is needed insidefunction(...), then change the parameter type accordingly.classB*toclassF*and use the result for anything, you are basically asking for undefined behavior.