Interview Question
Country: United States
Polymophism is the capability to change the behavior of a data type/structure. The easiest way is just to provide pointers to functions and allow for modification of the pointers. Override the pointers and point to a new function. Technically all C++ functions pass the data class as a parameter.
i..e
struct ChangeData
{
int data1;
};
typedef int (*CHANGE_ME)(ChangeData * param1);
int DefaultFn(ChangeData * param1) { return 0; }
CHANGE_ME &PtrChangeData = (CHANGE_ME)&DefaultFn;
My thought is to use a function pointer (that is, a callback) in place of an abstract class.
Take a polymorphic validator, for instance. It might be a PDF validator or a text validator, etc. But, the client code only knows the abstract version until runtime.
- Michael.J.Keating April 23, 2014