473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Address of a member function

Neo
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();
----
}

Now in its constructor-
foo::foo() {
x = <adddress_of_fa>;
}

how do I do the above?? I tried &(this->fa) but it says "C++ forbids
taking the address of a bound member function to form a pointer"
Thanks,
Neo

Aug 10 '07 #1
5 3099
Neo wrote:
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();
----
}

Now in its constructor-
foo::foo() {
x = <adddress_of_fa>;
}

how do I do the above?? I tried &(this->fa) but it says "C++ forbids
taking the address of a bound member function to form a pointer"
class foo {

typedef void (foo::*some_type)();

some_type x;

void fa();

foo() : x(&foo::fa) {}
};

--
Ian Collins.
Aug 10 '07 #2
Neo
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
Neo wrote:
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();
----
}
Now in its constructor-
foo::foo() {
x = <adddress_of_fa>;
}
how do I do the above?? I tried &(this->fa) but it says "C++ forbids
taking the address of a bound member function to form a pointer"

class foo {

typedef void (foo::*some_type)();

some_type x;

void fa();

foo() : x(&foo::fa) {}

};

--
Ian Collins.
I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?

thanks

Aug 10 '07 #3
Neo wrote:
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
....
I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?
It must be a static function only.
Aug 10 '07 #4
Neo
On Aug 10, 1:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
Neo wrote:
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
...
I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?

It must be a static function only.
thank you.

Aug 10 '07 #5
On Aug 10, 10:22 am, Neo <zingafri...@yahoo.comwrote:
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
Neo wrote:
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();
----
}
Now in its constructor-
foo::foo() {
x = <adddress_of_fa>;
}
how do I do the above?? I tried &(this->fa) but it says "C++ forbids
taking the address of a bound member function to form a pointer"
class foo {
typedef void (foo::*some_type)();
some_type x;
void fa();
foo() : x(&foo::fa) {}
};
I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?
The only thing you can use for type void(*)() is a free function
or a static function, with the correct signature.

In C++, the usual solution here would be to replace the
void(*)() with something like:

struct Callback
{
virtual ~Callback() {}
virtual void operator()() const = 0 ;
} ;

and use a Callback const*. If you have access to the sources
where the callback is used, do this. Otherwise, you're sort of
stuck.

Note too that if you pass the pointer to function to a C
function, the function itself has to be `extern "C"'. Which
means that even a static member function can't be used.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 11 '07 #6

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

Similar topics

3
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...
7
by: Howard | last post by:
Hi, in one of the recent posts, I saw someone pass two variables of built-in type (int and double), by reference, to a member function of a class. That function then took the addresses of those...
7
by: salmonella | last post by:
Hi, I'm trying to print on screen an address of class member function (the function of certain object of course). My code looks like this: class MyClass { public: void fun(void){}; };
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
8
by: gilad | last post by:
hi, I would like to get the address of a class method in an instance. In the following sample the third like access the method. how can I get the address into a long variable - something like -...
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...
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.