Template parameter (class) with a typedef member

T

ThazKool

Is there an elegant/efficient way to use a typedef of a class passed in
to a template parameter. I know that a second template parameter can
be given, but I was wondering if there is any way to avoid that.

Thanks,
ThazKool

Code:
template< class T >
class BTree
{
public:
void Query( T::Type value );
void Update( T::Type value, T::Type update );
void Delete( T::Type value );
void Insert( T node );
unsigned int Count() { return _count; };

private:
unsigned int _count;
};
 
L

Larry I Smith

ThazKool said:
Is there an elegant/efficient way to use a typedef of a class passed in
to a template parameter. I know that a second template parameter can
be given, but I was wondering if there is any way to avoid that.

Thanks,
ThazKool

Code:
template< class T >
class BTree
{
	public:
	void Query( T::Type value );
	void Update( T::Type value, T::Type update );
	void Delete( T::Type value );
	void Insert( T node );
	unsigned int Count() { return _count; };

	private:
	unsigned int _count;
};
[/QUOTE]

I don't understand what your attempting to do.
What is T::Type?  If 'T' is of class 'Apple',
then 'Insert( T node )' would become
'Insert( Apple node )'.  What do you want
'Query( T::Type value)' to be when 'T' is an
'Apple'?

Are you aware that std::set, std::multiset, std::map,
and std::multimap are (usually) backed by a red-black
tree?  You could use those, instead of implementing
your own B-Tree.

Regards,
Larry
 
J

Jonathan Mcdougall

Is there an elegant/efficient way to use a typedef of a class passed
in
to a template parameter. I know that a second template parameter can
be given, but I was wondering if there is any way to avoid that.

What do you mean? Doing something for T::Type? A simple

typedef typename T::Type Type;

will do.
Thanks,
ThazKool
Code:
template< class T >
class BTree
{
public:
void Query( T::Type value );
void Update( T::Type value, T::Type update );
void Delete( T::Type value );[/QUOTE]

These are all illegal.  Since Type is a type depending on a template
parameter, you must use 'typename':

void Query(typename T::Type value);

Your compiler may not be up to date.


Jonathan
 
K

Kai-Uwe Bux

ThazKool said:
Is there an elegant/efficient way to use a typedef of a class passed in
to a template parameter. I know that a second template parameter can
be given, but I was wondering if there is any way to avoid that.

Thanks,
ThazKool

Code:
template< class T >
class BTree
{
public:
void Query( T::Type value );
void Update( T::Type value, T::Type update );
void Delete( T::Type value );
void Insert( T node );
unsigned int Count() { return _count; };

private:
unsigned int _count;
};[/QUOTE]

Try:

template< class T >
class BTree
{
public:
void Query( typename T::Type value );
void Update( typename T::Type value, typename T::Type update );
void Delete( typename T::Type value );
void Insert( T node );
unsigned int Count() { return _count; };

private:
unsigned int _count;
};


Best

Kai-Uwe Bux

BTW: before implementing your own container type, you might think
about using the standard containers (unless you are doing this
just for fun, that is).
 
M

Matthias Kaeppler

Larry said:
Are you aware that std::set, std::multiset, std::map,
and std::multimap are (usually) backed by a red-black
tree? You could use those, instead of implementing
your own B-Tree.

A B-Tree is not necessarily a binary tree. Actually, in most
cases it isn't, because B-Trees ought to expand in breadth, not in
height (binary trees would grow too high to implement an efficient data
lookup in a database for example).
A red-black tree is always a binary tree however.
 
T

ThazKool

Thank you all. I am researching the typename keyword works well. I
should have posted all of the code. It is listed below. It seems as
though my Btree is not really a Btree. Anyway, I am doing this for
some fun learning.

Thanks,
ThazKool

template< class T >
class BTree
{
public:
void Query( typename T::Type value );
void Update( typename T::Type value, typename T::Type update );
void Delete( typename T::Type value );
void Insert( T node );
unsigned int Count() { return _count; };

private:
unsigned int _count;
};

template< typename T >
class BTreeNode
{
public:
//BTreeNode( value, left, middle, right );
typedef T Type;

private:
BTreeNode<T>* _left;
BTreeNode<T>* _middle;
BTreeNode<T>* _right;
T _value;
};
 
T

ThazKool

I am interested in learning more about data structures for efficient
data lookup for databases. Do you know any good informative links on
the subject?

Thanks,
ThazKool
 
M

Matthias Kaeppler

ThazKool said:
I am interested in learning more about data structures for efficient
data lookup for databases. Do you know any good informative links on
the subject?

Thanks,
ThazKool

Hm, not really, sorry. But you can read about B and B+ trees in about
every book on database systems, they're pretty common.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top