Sign in to follow this  

Virtual function question.

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

}


Share this post


Link to post
Share on other sites

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this