473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to a template function

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
Jul 22 '05 #1
2 2154
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.
Jul 22 '05 #2

"Chris Schadl" <cs*****@satan.blah.org.uk> wrote in message
news:pa****************************@satan.blah.org .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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2927
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
4
2117
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
4
4245
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
15
2205
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
11
3407
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
5
2251
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
35490
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
3
2429
by: .rhavin grobert | last post by:
guess you have the following: _________________________________________________ template <class T> class CQVector { public: // find an element, returns index or -1 if none is found int...
50
4429
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
7
3792
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.