"Valid" is a specific term when applied to pointers. Data pointers are valid when they point to an object or NULL
; function pointers are valid when they point to a function or NULL
, and pointers to members are valid when the point to a member or NULL
.
However, from your question about actual output, I can infer that you wanted to ask something else. Let's look at your vmember
function - or should I say functions? Obviously there are two function bodies. You could have made only the derived one virtual, so that too confirms that there are really two vmember
functions, who both happen to be virtual.
Now, the question becomes whether when taking the address of a member function already chooses the actual function. Your implementations show that they don't, and that this only happens when the pointer is actually dereferenced.
The reason it must work this way is trivial. Taking the address of a member function does not involve an actual object, something that would be needed to resolve the virtual call. Let me show you:
namespace {
void (A::*test)() = &A::vmember;
A a;
B b;
(a.*test)();
(b.*test)();
}
When we initialize test
, there is no object of type A
or B
at all, yet is it possible to take the address of &A::vmember
. That same member pointer can then be used with two different objects. What could this produce but "In A::vmember()\n" and "In B::vmember()\n" ?