Thread: Address of a function

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Address of a function

    How can we collect the address of a function in C++ into a function pointer? Please help me modify the following code so as to collect the address of display() in p.

    Code:
    #include<iostream.h>
    class ex
    {
    	int i,j;
    public: 
    
    	ex(int ii,int jj)
    	{
    		i=ii;j=jj;
    	}
    	ex()
    	{
    		i=0;
    	}
    	display()
    	{
    		cout<<i<<endl<<endl;
    	}
    };
    
    void main()
    {
    	ex e(1,2); void (*p)();
    	p=&(e.display());
    	cout<<e.display()<<endl<<p<<endl;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You've been told these things before but...
    1) "#include <iostream>" along with "using namespace std;"
    2) "int main()" and "return 0;" at the end.

    About your problem, I think you should use:
    Code:
    p = e.display;
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    It says
    line (24) : " '=' : cannot convert from 'void (__thiscall ex::*)(void)' to 'void (__cdecl *)(void)'"
    Code:
    #include<iostream>
    class ex
    {
    	int i,j;
    public: 
    
    	ex(int ii,int jj)
    	{
    		i=ii;j=jj;
    	}
    	ex()
    	{
    		i=0;
    	}
    	void display()
    	{
    		std::cout<<i;
    	}
    };
    
    int main()
    {
    	ex e(1,2); void (*p)();
    	p=e.display;
    	std::cout<<p;
    return 0;
    }
    Last edited by juice; 09-30-2011 at 04:41 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Then declare p as:
    Code:
    void (ex::*p)();
    ( I've never tried it myself, so don't start chasing me if it fails. )
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Pointers to non-static member functions of a class are different types from pointers to other functions.

    Try explaining what you are actually trying to achieve. Once you have "collected" the address of a function, what do you expect to be able to do with it?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I wanna see the address of display. I made that change suggested by Greaper and the program compiled with one warning, but its not printing the address of display, instead it is printing out integer 1.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A pointer to a member is not (necessarily) an address. The precise representation is implementation-specific, but is typically an offset relative to an object, not an address. That is why the syntax to call a pointer to member requires both an object as well as the pointer-to-member-function.

    That is why I was asking you what you REALLY sought to achieve. Your question was meaningless. Your clarification still is.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I need to print the address of the function display()...

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And, if as grumpy says, you can't?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to specify address for a function
    By pilgrim in forum Linux Programming
    Replies: 9
    Last Post: 09-28-2010, 12:20 PM
  2. Address of a Function
    By jrahhali in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2009, 12:59 PM
  3. What is the true address of a function ptr?
    By Raigne in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2008, 12:24 PM
  4. taking Function's address
    By arjunajay in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2005, 05:55 AM
  5. Passing an address to a function
    By Vicious in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2004, 12:05 PM