Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to a template function

Reply
Thread Tools

Pointer to a template function

 
 
Chris Schadl
Guest
Posts: n/a
 
      04-20-2004
Hi,

I'm working on writing a templated Binary Search Tree class in C++. In
regular C, I would have functions which traverse the tree take a pointer
to a user-defined function as an argument, which would then execute the
user-defined function for each node that is "visited". For example:

/* in the BST header file */
/* I actually don't remember if this is valid C or not */
typedef void (*fn)(void* tree_item);
void BST_inorder_traverse(const BST *tree, fn func);
/*...*/
void display_item(void* tree_item);
/*...*/
BST_inorder_traverse(tree, display); /* or something */

However, I'm not entierly certian how to do this in C++ if the BST is a
templated class. Is it possible to use typedef in conjunction with
templates? or is there some other 'correct' way of doing something like
this in C++?

Thanks,

Chris Schadl
 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      04-20-2004
Chris Schadl wrote:

> I'm working on writing a templated Binary Search Tree class in C++. In
> regular C, I would have functions which traverse the tree take a pointer
> to a user-defined function as an argument, which would then execute the
> user-defined function for each node that is "visited". For example:
>
> /* in the BST header file */
> /* I actually don't remember if this is valid C or not */
> typedef void (*fn)(void* tree_item);
> void BST_inorder_traverse(const BST *tree, fn func);
> /*...*/
> void display_item(void* tree_item);
> /*...*/
> BST_inorder_traverse(tree, display); /* or something */
>
> However, I'm not entierly certian how to do this in C++ if the BST is a
> templated class. Is it possible to use typedef in conjunction with
> templates? or is there some other 'correct' way of doing something like
> this in C++?


The standard library uses function objects. See for example the sort
template. You use templates to avoid specifying the type of a function
object. Here's a simple example.

#include <ostream>
#include <iostream>

template <typename Function>
void example (Function func)
{
for (int i = 0; i != 10; ++ i) func (i);
}

void f (int i)
{
std::cout << "Print " << i << " through pointer-to-function.\n";
}

struct g
{
void operator () (int i)
{
std::cout << "Print " << i << " through function object.\n";
}
};

int main ()
{
example (& f);
example (g ());
}

--
Regards,
Buster.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-20-2004

"Chris Schadl" <> wrote in message
news .uk...
> Hi,
>
> I'm working on writing a templated Binary Search Tree class in C++. In
> regular C, I would have functions which traverse the tree take a pointer
> to a user-defined function as an argument, which would then execute the
> user-defined function for each node that is "visited". For example:
>
> /* in the BST header file */
> /* I actually don't remember if this is valid C or not */
> typedef void (*fn)(void* tree_item);
> void BST_inorder_traverse(const BST *tree, fn func);
> /*...*/
> void display_item(void* tree_item);
> /*...*/
> BST_inorder_traverse(tree, display); /* or something */
>
> However, I'm not entierly certian how to do this in C++ if the BST is a
> templated class. Is it possible to use typedef in conjunction with
> templates?


Of course, assuming something like this, where NODE is the type of the node
data (and so the type of the parameter to your function pointer).

template <class NODE>
class BST
{
public:
typedef void (*node_visitor_func)(NODE*);
void inorder_traverse(node_visitor_func func);
};

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:04 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments