template class

Aug 3, 2010 at 3:11am
I am working on a software project and I don't understand the following c++ function:

1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
user_type_t foo(T* object, user_type_t(T::*method)(event*))
{
    T::ATTEMPING_TO_DO_SOMETHING();
    return goo(object, (user_method_t)method);
}

user_type_t goo(user_object_t object, user_method_t method)
{
    //so something
    return S_TRANSITION;
}


I don't know what T::*method mean here, please help.

Thanks in advance,

aayu09

Aug 3, 2010 at 3:30am
It's a member function pointer, in this case a pointer to a method of the class "T" that takes a pointer to an event and returns a user_type_t. Search "pointer to member function" for more info on it, their syntax and semantics are fairly complicated.
Aug 3, 2010 at 3:38am
http://www.cplusplus.com/doc/tutorial/templates/
Basically, a template allows you to create a class or function with generic types. In this example, T is replaced with whatever you place in between the <>'s:

foo<myType>(myObject,myFunction);

This lets you use any type for myObject.
Aug 3, 2010 at 3:41am
Thanks a lot, I got it.
Aug 3, 2010 at 12:57pm
BTW, this line: return goo(object, (user_method_t)method);

almost certainly does not work in all cases if the explicit cast is necessary.

Topic archived. No new replies allowed.