Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
(c++) Pointer-to-member function stored in STL map

 
Post new topic   Reply to topic Printable version
 View previous topic  Sqlite String Escaping Post :: Post My CAPTCHA image decoding quest!  View next topic  
Author Message
grazzhoppa
Novice


Joined: Jan 03 2007
Posts: 29
Offline

PostPosted: Thu Aug 07, 2008 12:32 am    Post subject: (c++) Pointer-to-member function stored in STL map Reply to topic Reply with quote

edit: I solved it, but would like an explanation.

What is the syntax for invoking a function stored as a value in STL map as a pointer-to-member function, through a map<>::iterator.

STL map Class member declaration
Code: Show/Hide

#include <map>
using std::map;

class UserData
{
     map<long, void (UserData::*)(CORE_DATA *cd)>    *timersBot;

    /* timersBot is a pointer to an STL map<>
     * that has an integer as the key,
     * and a pointer-to-a member function of UserData-class as the value
     */
}

Class being used in code
Code: Show/Hide

    ...

    map<long, void (UserData::*)(CORE_DATA *cd)>::iterator    iter = /* assume the iterator is valid */
    CORE_DATA *cd = /* assume this is valid */

    /* What I've tried: */
    (iter->*second)(cd);          // compiler error " 'second' was not declared in this scope"
    (*(iter->second))(cd);        // compiler error " invalid use of `unary *' on pointer to member"

    UserData *ud = /* assume this is valid */
    ud->*(iterBotT->second)(cd);  // compiler error " must use .* or ->* to call pointer-to-member function .....

    /*
     * How can invoke the function from the pointer-to-function
     * that's stored in   iter->second  ?
     */

    (ud->*iterBotT->second)(cd);  // IT COMPILES but why?

    ...

Back to top
View users profile Send private message Add User to Ignore List
Snrrrub
Novice


Joined: May 29 2008
Posts: 37
Offline

PostPosted: Fri Aug 08, 2008 12:25 pm    Post subject: Reply to topic Reply with quote

Every invocation of a member function is associated with an object. Consider:

Player p;
p.SendMessage("hello world");

In this case, SendMessage is invoked on the object 'p'. In the implementation of Player::SendMessage, you can refer to the object 'p' through the pointer 'this'. It's useful to imagine that every invocation of a member function in C++ involves a hidden 'this' argument.

Function pointers to member functions are no different. You still have to specify *which* object you're invoking the method on. That's why your first two attempts didn't succeed. When you store a function pointer, you're ONLY storing the function pointer, not the <object, function pointer> pair that you need to make a successful call.

ud->*(iterBotT->second)(cd);

The code above fails because of operator precedence: in this case, the function call has higher precedence than the member dereference operation so it thinks you're attempting to take the return value of the function call and dereference that in the object 'ud'.

The example that compiles works because you've:
1) specified the object on which to call the member function ('ud')
2) you've dereferenced the member function correctly
3) applied the function call AFTER dereferencing the member function (the parentheses around ud->*iterBotT->second)

Note that -> has a higher precedence than * so when you say *iterBotT->second, it really means *(iterBotT->second) so you get the expected results.

This is a good site to learn more about function pointers: http://www.newty.de/fpt/index.html
A good C++ operator precedence and associativity table is here: http://cs.smu.ca/~porter/csc/ref/cpp_operators.html

-Snrrrub
Back to top
View users profile Send private message Add User to Ignore List
grazzhoppa (no password)
Guest


Offline

PostPosted: Sat Aug 09, 2008 12:46 am    Post subject: Reply to topic Reply with quote

Thank you, I understand it now. You were very clear icon_smile.gif

It helps me to think that pointers-to-member functions are more like an offset rather than an actual memory address.

(Class->*pointer-to-member_function)(arguments)
Would be like saying "execute the member function of this particular Class object. The member function has the memory address equal to Class's base address plus the offset specified by the "pointer-to-member_function." I'm not sure if this is what's happening in reality, but it helps me to think of it that way.

If pointer-to-member functions were actual memory addresses, then there would be a common mistake of trying to execute a member function of a object that had been destroyed since storing the function pointer.
Back to top
Siva
Guest


Offline

PostPosted: Thu Jul 30, 2009 3:11 pm    Post subject: Broader question Reply to topic Reply with quote

That is a very detailed explanation.

I have a follow up question.

What could be reason's that the object reference of a FP
is not defaulted to the current object scope,
why an explicit use of *this* is needed.
Back to top
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Jul 30, 2009 6:18 pm    Post subject: Reply to topic Reply with quote

my guess is that a function pointer is just an address, and the address of the function is shared for all instances of the class (which secretly pass in a base pointer to the data for the instance of the class when you call it). It may be possible the user intended to call the function on a different instance of the class, so you must be explicit.
_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
john310
Newbie


Joined: Apr 26 2011
Posts: 1
Offline

PostPosted: Tue Apr 26, 2011 6:05 am    Post subject: Reply to topic Reply with quote

In fact, the STL's map class allows you to store data by any type of key instead of simply by a numerical key, the way you must access an array or vector. So instead of having to compute a hash function and then access an array, you can just let the map class do it for you.

To use the map class, you will need to include <map> and maps are part of the std namespace. Maps require two, and possibly three, types for the template:

std::map <key_type, data_type, [comparison_function]>

Notice that the comparison function is in brackets, indicating that it is optional so long as your key_type has the less-than operator, <, defined -- so you don't need to specify a comparison function for so-called primitive types such as int, char, or even for the string class. Moreover, if you overloaded the < operator for your own class, this won't be necessary.

The reason that the key type needs the less-than operator is that the keys will be stored in sorted order -- this means that if you want to retrieve every key, value pair stored in the map, you can retrieve them in the order of the keys.

Let's go back to the example of storing student grades. Here's how you could declare a variable called grade_list that associates strings (student names) with characters (grades -- no + or - allowed!).

std::map <string, char> grade_list;
_________________
Alan
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Tue Apr 26, 2011 10:22 pm    Post subject: Reply to topic Reply with quote

Good job, bot and/or retarded exploited child, you can copy paste a webpage:
http://www.cprogramming.com/tutorial/stl/stlmap.html


Give him hell, MGB! tongue.gif
_________________
(Insert a bunch of dead links here)
Back to top
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 449 page(s) served in previous 5 minutes.

phpBB Created this page in 0.464772 seconds : 33 queries executed (90.9%): GZIP compression disabled