473,887 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12128
"Roy Yao" <fl***@263.ne t> 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.********@att Abi.com> wrote in message
news:d%gdb.4441 08$cF.142132@rw crnsc53...
"Roy Yao" <fl***@263.ne t> 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.ne t> 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<MyC lass *>(p)->f(); }

int main () {
MyClass obj;
do_callback(cal l_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<Bas e *>(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(cal l_f, &obj);

// RIGHT: pass Base * as expected
do_callback(cal l_f, static_cast<Bas e *>(&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_callbac k (cb_func_t f, Base *x) { do_callback(f, x); }

int main () {
Derived obj;
do_base_callbac k(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
665
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 member function?
3
3777
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 data in my vtable? RTTI could be implemented this way instead of as a special case for the compiler. It would also be useful for class specific memory management implementations. -- Daniel A. Graifer
22
2976
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 me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
2
1565
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 function. In my code i get the address of class A (i can't get the address of the other classes, its children) and then call the vertual function. It then in turn calls the vertual function of class C. Is there any way of getting the address of...
11
4380
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 it inline. Like this.
2
2063
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 error C1001: internal compiler error. // (File "msc1.cpp", Line 1392)
14
4237
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
5
3128
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 than making the func static? For ex: class foo { some_type x void fa();
11
1903
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
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.