Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2007, 11:02 AM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4 Klarre is on a distinguished road
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
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 27th, 2007, 9:48 PM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Mar 28th, 2007, 1:07 AM   #3
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4 Klarre is on a distinguished road
Quote:
Originally Posted by Game_Ender View Post
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
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 28th, 2007, 6:34 AM   #4
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining languages titaniumdecoy Other Programming Languages 12 Jul 13th, 2006 2:03 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
libraries matko C 1 Jan 22nd, 2006 2:12 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:38 AM.

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