Virtual function question.
Suppose I have the following code:
class A {
public:
virtual void DoSomething();
}
class B: public A {
public:
void DoSomething();
}
void (*pFunc)();
B Obj;
A * pObj = &B
pFunc = pObj->DoSomething;
Will calling pFunc actualy call DoSomething of class B? or class A?
[Edited by - The C modest god on August 26, 2004 6:49:27 AM]
have you ever tried that? it won't compile, because you can't cast a pointer to a member function to a pointer to a C function, because they use different calling conventions.
in C++ a member function always copies the "this"-pointer on the stack, and therefore "this" is the first parameter of the function. in C you have no such "this" pointer. so you can't cast between them.
chris
in C++ a member function always copies the "this"-pointer on the stack, and therefore "this" is the first parameter of the function. in C you have no such "this" pointer. so you can't cast between them.
chris
Nope. In fact, that won't even compile.
You can only assign static member functions to a regular function pointer.
From function-pointer.org:
Regarding their syntax, there are two different types of function pointers: On the one hand there are pointers to ordinary C functions or static C++ member functions, on the other hand there are pointers to non-static C++ member functions. The basic difference is that all pointers to non-static member functions need a hidden argument: The this-pointer to an instance of the class. Always keep in mind: These two types of function pointers are incompatible with each other.
http://www.function-pointer.org
You can only assign static member functions to a regular function pointer.
From function-pointer.org:
Regarding their syntax, there are two different types of function pointers: On the one hand there are pointers to ordinary C functions or static C++ member functions, on the other hand there are pointers to non-static C++ member functions. The basic difference is that all pointers to non-static member functions need a hidden argument: The this-pointer to an instance of the class. Always keep in mind: These two types of function pointers are incompatible with each other.
http://www.function-pointer.org
Example of pointer to member function with both normal indirect call & indirect call with polymorphic behvaiour:
#include <iostream>struct A { virtual void do_it_with_magic() const = 0; virtual ~A() {}};struct B : A { void do_it_no_magic() const { std::cout << "B IS DOING IT NO MAGIC\n"; } void do_it_with_magic() const { std::cout << "B is doing it by magic!! polymorphic power\n"; }};int main() { //normal pointer to member function typedef void (B::*b_ptr_func)() const; B b; b_ptr_func bf = &B::do_it_no_magic; //direct call b.do_it_no_magic(); //in-direct call (b.*bf)(); // pointer to member function with polymorphic behaviour typedef void (A::*a_ptr_func)() const; a_ptr_func f = &A::do_it_with_magic; A* g = new B; //polymorphic direct call g->do_it_with_magic(); //polymorphic indirect call (g->*f)(); delete g; return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement