Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Virtual function question.

This topic is 5164 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.


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.


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

//in-direct call

// 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

//polymorphic indirect call

delete g;

return 0;


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!