www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function name from function pointer

reply "Paul D Anderson" <claude.reins msnmail.com> writes:
Is there a way to return the name of a function (a string) from a 
pointer to that function?

Function pointer example from D Reference:
---
int function() fp;

void test()
{
     static int a = 7;
     static int foo() { return a + 3; }

     fp = &foo;
}

void bar()
{
     test();
     int i = fp();       // i is set to 10
}
---

Can I get "foo" from fp?

Paul
Apr 11 2015
next sibling parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Sat, 11 Apr 2015 18:28:35 +0000
schrieb "Paul D Anderson" <claude.reins msnmail.com>:

 Is there a way to return the name of a function (a string) from a 
 pointer to that function?
 
 Function pointer example from D Reference:
 ---
 int function() fp;
 
 void test()
 {
      static int a = 7;
      static int foo() { return a + 3; }
 
      fp = &foo;
 }
 
 void bar()
 {
      test();
      int i = fp();       // i is set to 10
 }
 ---
 
 Can I get "foo" from fp?
 
 Paul
 
 
Nope, that would require that fp not only contains a pointer to the function but also a pointer to the name. That's not how it works. But continuing that thought, you could add the function's name as an additional variable and set that every time you set fp. -- Marco
Apr 11 2015
parent "Paul D Anderson" <claude.reins msnmail.com> writes:
On Saturday, 11 April 2015 at 19:08:50 UTC, Marco Leise wrote:
 Am Sat, 11 Apr 2015 18:28:35 +0000
 schrieb "Paul D Anderson" <claude.reins msnmail.com>:

 Is there a way to return the name of a function (a string) 
 from a pointer to that function?
 
 Function pointer example from D Reference:
 ---
 int function() fp;
 
 void test()
 {
      static int a = 7;
      static int foo() { return a + 3; }
 
      fp = &foo;
 }
 
 void bar()
 {
      test();
      int i = fp();       // i is set to 10
 }
 ---
 
 Can I get "foo" from fp?
 
 Paul
 
 
Nope, that would require that fp not only contains a pointer to the function but also a pointer to the name. That's not how it works. But continuing that thought, you could add the function's name as an additional variable and set that every time you set fp.
Okay, thanks, I can see that. Paul
Apr 11 2015
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Paul D Anderson:

 Is there a way to return the name of a function (a string) from 
 a pointer to that function?
Perhaps creating a string[void*] AA and initializing with all the function pointers you care about. Bye, bearophile
Apr 11 2015