Alf P. Steinbach said:
* mohan:
Don't use void pointers: you lose type checking, i.e. you introduce bugs.
See <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>,
section 1.2 on "Run-time polymorphism".
He said C, not C++.
I've seen something similar to polymorphism done in C with function
pointers. Although it didn't use void pointers, which are a bad thing, but
function pointers.
I may have the syntax off on this.
typedef (void MyFunc*)(int, float);
Which should (check syntax) create a function pointer called MyFunc which
points to a function returning a void taking 2 parameters, int and float.
Then you can at run time point this pointer to different functions depending
on what you want to do.
void MyRealFunc( int SomeInt, float SomeFloat)
{
std::cout << "In MyRealFunc" << std::endl;
}
void MyRealFunc2( int SomeInt, float SomeFloat)
{
std::cout << "In MyRealFunc2" << std::endl;
}
int main()
{
MyFunc = MyRealFunc;
MyFunc(1, 2.0);
MyFunc = MyRealFunc2;
MyFunc(1, 2.0);
return 0; // Not needed in main
}
Although this concept only works for functions, not classes as it would in
C++.
Thinking about it, I wonder if you couldn't create a pointer to a structure,
and the structure could have variables and function pointers itself
emulating C++'s class.