472,954 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

How can I get the address of a virtual member function?

Hello,

I need to pass a pointer to a
callback function to the lower
level modules. But the function
is thought to be a virtual member one.

How can I get the real address of
the virtual member function?

Best regards,
Roy
Jul 19 '05 #1
3 11835
"Roy Yao" <fl***@263.net> wrote...
I need to pass a pointer to a
callback function to the lower
level modules. But the function
is thought to be a virtual member one.

How can I get the real address of
the virtual member function?


There is no difference of getting the address
of a member function based on its virtuality.
The way the address is used will determine
which function is actually called. The syntax
for getting the address of a member is

& <class-name> :: <member-name>

for example:

struct A {
virtual int foo() { return 42; }
};

struct B : A {
int foo() { return 73; }
};

int main() {
B b;
A& a = b;
int (A::*amem)() = &A::foo; // take address
return (a.*amem)(); // use the address
}

(the program above should return 73 to the hosting
environment)

BTW, what problem are you encountering?

Victor
Jul 19 '05 #2
Hello, Victor,

Thanks at first.

The problem troubling me is that I must convert a virtual member function
pointer to a non-member function pointer in order that other modules can
call the virtual member function without an object( that is the "this"
pointer is not needed in my virtual member function).

Can you catch my idea?

Roy

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:d%gdb.444108$cF.142132@rwcrnsc53...
"Roy Yao" <fl***@263.net> wrote...
I need to pass a pointer to a
callback function to the lower
level modules. But the function
is thought to be a virtual member one.

How can I get the real address of
the virtual member function?


There is no difference of getting the address
of a member function based on its virtuality.
The way the address is used will determine
which function is actually called. The syntax
for getting the address of a member is

& <class-name> :: <member-name>

for example:

struct A {
virtual int foo() { return 42; }
};

struct B : A {
int foo() { return 73; }
};

int main() {
B b;
A& a = b;
int (A::*amem)() = &A::foo; // take address
return (a.*amem)(); // use the address
}

(the program above should return 73 to the hosting
environment)

BTW, what problem are you encountering?

Victor

Jul 19 '05 #3
"Roy Yao" <fl***@263.net> wrote in message news:<bl***********@mail.cn99.com>...
The problem troubling me is that I must convert a virtual member function
pointer to a non-member function pointer
You cannot do this in C++.
in order that other modules can
call the virtual member function without an object( that is the "this"
pointer is not needed in my virtual member function).


First, a member function which does not use the "this" pointer should
not be a member function (or, at least, it should be a static member
function). Second, all virtual function calls use the "this" pointer
to determine which implementation to call.

As such, you can do what you want by breaking up the virtual function
into a static member function and a virtual function which calls it:

class Base {
public:
virtual void f () = 0;
virtual ~Base ();
};

class Derived : public Base {
public:
static void do_f ();
void f () { do_f(); }
};

Then pass &Derived::do_f instead.

However, that's not how you'd normally deal with a properly-written
C-style callback API. Such an API would take a pointer to a function
and a pointer to data to be passed to that function:

typedef void *cb_data_t;
typedef void (*cb_func_t) (cb_data_t);
void do_callback (cb_func_t f, cb_data_t x); /* calls f(x) */

To this API, pass a pointer to an object and a pointer to either a
static member or a non-member function which calls the desired member
function on the object:

class MyClass {
public:
void f ();
};

void call_f (cb_data_t p) { static_cast<MyClass *>(p)->f(); }

int main () {
MyClass obj;
do_callback(call_f, &obj);
return 0;
}

Note that you have to be really careful when you do this with
inheritance:

class Base {
public:
virtual void f () = 0;
virtual ~Base ();
};

void call_f (cb_data_t p) { static_cast<Base *>(p)->f(); }

class Derived : public Base {
public:
virtual void f ();
};

int main () {
Derived obj;

// WRONG: call_f expects Base *; we're passing Derived *
do_callback(call_f, &obj);

// RIGHT: pass Base * as expected
do_callback(call_f, static_cast<Base *>(&obj));
return 0;
}

In theory, conversion of Derived * to void * to Base * is undefined.
In practice, it probably will work unless multiple or virtual
inheritance is used. In any case, I suggest that you not take any
chances -- convert to Base * before passing. If you don't like casts
(who does?), you can write a wrapper like this instead:

// guarantees that x is a Base *
void do_base_callback (cb_func_t f, Base *x) { do_callback(f, x); }

int main () {
Derived obj;
do_base_callback(call_f, &obj);
return 0;
}

- Shane
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
2
by: scott | last post by:
Lets say i have class A class B and class C. Class C inherites class B and class B inherites class A. Class A contains a vertual function (not pure) and class C contains the same vertual...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
2
by: pascal.zschumme | last post by:
hello folks My problem is that the following code using something very very difficult technique :D fails to compile on MSVC8: <code> // main.cpp // // the error is: // main.cpp(8) : fatal...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
5
by: Neo | last post by:
hi, I searched in the groups here but didnt find any post answering my question. My question is I need the address of the member function in the same class. Is there anyway to go about it other...
11
by: Gabriel de Dietrich | last post by:
Hi all! Just out of curiosity: Is there any way to get the address of a particular virtual member function given an object? Some code to make things more clear... class A { public: A() { }
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.