Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Get member function address (http://www.programmingforums.org/showthread.php?t=12887)

Klarre Mar 27th, 2007 12:02 PM

Get member function address
 
Is there an easy way to get the memory address to a member function? I don't really trust (understand) the output of this code...

:

#include <iostream>

class Foo
{
public:
        void func()
        {
                std::cout << func << std::endl;
        }

        void func2()
        {
                std::cout << func2 << std::endl;
        }
};

int main()
{
        Foo foo;
        foo.func();
        foo.func2();

        return 0;
}

:

Output:
1
1


Why does it writes "1" to the screen in both functions? How do I make it print the real memory address?

Thanks for your help!

/Klarre

Game_Ender Mar 27th, 2007 10:48 PM

A member function doesn't have a unique address for each object, you need both a pointer to the member function an object to call it with. The the C++ FAQ.

Klarre Mar 28th, 2007 2:07 AM

Quote:

Originally Posted by Game_Ender (Post 125865)
A member function doesn't have a unique address for each object.

Of course. But still the function have to be somewhere in the memory space, at a specific memory address. Guess I will need some assembly for this, though the "pointer-to-member" pointer works in a different manner than "pointer-to-function" does. Thanks for the link!

/Klarre

Eoin Mar 28th, 2007 7:34 AM

I believe you need to fully qualify the function name, i.e. &Foo::member_func. Here is a note from Microsoft on it-

Quote:

Pointer-to-members now require qualified name and &

Code written for previous versions of the compiler that just used the method name will now give Compiler Error C3867 or Compiler Warning C4867. This diagnostic is required by Standard C++. Now, to create a pointer to a member function, the address of operator (&) must be used with the fully qualified name of the method. Not having to use the & operator and the fully qualified name of the method can lead to logic bugs in code due missing parentheses in function calls. Using the function's name without an argument list results in a function pointer which is convertible to several types. This code would have compiled, leading to unexpected behavior at runtime.


All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC