C++ polymorphism functions taking void *
and other pointer type as their arguments: is it considered ambiguous?
I am worried that since any pointer can be cast to void*
, will the 2nd call of bar below executes void bar(void*)
instead of my expected void bar(int*)
, in the program below?
I tested on my g++, and it runs as expected (i.e. int* won't be cast to void*). But can anyone comment/answer on/to this question in the respect of C++ language specifications?
foo.h:
class Foo {
public:
void bar(void *);
void bar(int *);
};
main.cpp:
...
struct A *p1;
int *p2;
Foo foo;
...
foo.bar(p1);
foo.bar(p2);
Furthermore, say, bar
are now virtual polymorphism functions, taking void*
argument as the 1st form, and base abstract class pointer argument as the 2nd form. Will a call with a derived class pointer as the argument execute the 1st form or the 2nd form? i.e. Will the derived class pointer been cast to its base abstract class pointer (and thus the 2nd form will be in action), or will it be cast to void *
(and thus the 1st form will be in action) before calling bar()
?