473,387 Members | 1,650 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,387 software developers and data experts.

how to take Address of a Member Function in C++

I want to take address of a member function in c++.
how to do this.
please suggest me as soon as possible.
Oct 23 '13 #1
2 1823
Banfa
9,065 Expert Mod 8TB
Something like this

Expand|Select|Wrap|Line Numbers
  1. class Example
  2. {
  3. public:
  4.   int function(double);
  5. }
  6.  
  7. int main()
  8. {
  9.   // Declare memFunPtr, pointer to a member function
  10.   int (Example::*memFunPtr)(double) = &Example::function;
  11.  
  12.   // Use the member function pointer
  13.  
  14.   int result;
  15.   double input = 5.0;
  16.   Example e;
  17.  
  18.   result = (e.*memFunPtr)(input);
  19. }
  20.  
To avoid syntax errors it is a good idea to typedef your member function pointer type

Expand|Select|Wrap|Line Numbers
  1. class Example
  2.   typedef int (Example::*MemFunPtr_t)(double);
  3.  
  4.   MemFunPtr_t memFunPtr2 = &Example::function;
  5.  
Oct 23 '13 #2
sir actually i havn't got your point.
So please explain in detail.
Suppose A is a class and Display is a Member function of class A. if i want to see the address of Display member function then why i can't write like below code.
Expand|Select|Wrap|Line Numbers
  1. A obj;
  2. cout<<&obj.Display();    
  3.  
  4.  
  5.  
why it gives error.
Oct 24 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
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...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: Brian Riis | last post by:
Given: template <typename T1, typename T2> class A { public: template <int I> void B(); };
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
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...
4
by: Paul Jansen | last post by:
Don't go away yet... this is a bit more complicated than 'how do I take a method address'! I have a class foo, and method foo::bar. I also have a tree of foo objects, which I process...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
3
by: Dmitry | last post by:
Hi all, Consider the following code: class A { public: A() {} void DoMethod() { (this->*m_pMethod)(); } protected: virtual void MethodA() { ... }
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.