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_tra verse(const BST *tree, fn func);
/*...*/
void display_item(vo id* tree_item);
/*...*/
BST_inorder_tra verse(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 2170
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_tra verse(const BST *tree, fn func); /*...*/ void display_item(vo id* tree_item); /*...*/ BST_inorder_tra verse(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.bla h.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_tra verse(const BST *tree, fn func); /*...*/ void display_item(vo id* tree_item); /*...*/ BST_inorder_tra verse(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_travers e(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:
http://www.function-pointer.org/
|
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 classes
|
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 complementary to one
another.
So I proceeded to decompose the tasks that deal with user options into
two functions. Each of the functions do something and towards the end
they do supplementary tasks that depend on the server option.
The whole things...
|
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 nodes with the following code, but I don't know how
to sum them up and return that value to the calling function in main().
I know the algorithm is recursive in nature, but I get gibberish for
|
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 exception
neutral/
I got a reply from Bux with his code for a smart pointer with far fewer
lines of code
and more cleaner design, not over engineered like mine.
...
| |
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 to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are calling a function very
frequently for evaluation reason: think of the problem of performing...
|
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 implementation so the two can vary independently.
Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
|
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 find(int id) const;
private:
|
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):
//------------------------------------------------------------------
template<typename Data_t>
class SmartPointer
{
Data_t* data;
void(*deleterFunc)(Data_t*);
|
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 event);
so I declared a function pointer like
typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |