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 2 2143
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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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):
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |