简体   繁体   中英

How to pass a generic argument to a function pointer?

I am trying to pass a function pointer to another function with an argument of type T.

Like so:

void preorder ( void ( *functptr ) (T&) ) // preorder traversal of tree
{
    preorder(root, functptr(T));

}

I understand that I need to pass an actual object to the function but this is being passed to another private function that actually knows what function it will be working with and I just need this public method to be a gateway between the two.

Try

template <typename T>
void preorder ( void ( *functptr ) (T&) )
{
    preorder(root, functptr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM