| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"ThazKool" |
| Date: |
10 May 2005 10:20:07 PM |
| Object: |
Template parameter (class) with a typedef member |
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;
};
.
|
|
| User: "Jonathan Mcdougall" |
|
| Title: Re: Template parameter (class) with a typedef member |
10 May 2005 11:16:20 PM |
|
|
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 );
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
.
|
|
|
|
| User: "ThazKool" |
|
| Title: Re: Template parameter (class) with a typedef member |
11 May 2005 01:23:09 AM |
|
|
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;
};
.
|
|
|
|
| User: "Larry I Smith" |
|
| Title: Re: Template parameter (class) with a typedef member |
10 May 2005 10:38:11 PM |
|
|
ThazKool wrote:
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;
};
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
--
Anti-spam address, change each 'X' to '.' to reply directly.
.
|
|
|
| User: "Matthias Kaeppler" |
|
| Title: Re: Template parameter (class) with a typedef member |
11 May 2005 12:50:29 AM |
|
|
Larry I Smith wrote:
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.
--
Matthias Kaeppler
.
|
|
|
| User: "ThazKool" |
|
| Title: Re: Template parameter (class) with a typedef member |
11 May 2005 01:25:12 AM |
|
|
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
.
|
|
|
| User: "Matthias Kaeppler" |
|
| Title: Re: Template parameter (class) with a typedef member |
11 May 2005 06:27:01 AM |
|
|
ThazKool wrote:
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.
--
Matthias Kaeppler
.
|
|
|
|
|
|
|
| User: "Kai-Uwe Bux" |
|
| Title: Re: Template parameter (class) with a typedef member |
10 May 2005 11:32:16 PM |
|
|
ThazKool wrote:
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;
};
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).
.
|
|
|
|

|
Related Articles |
|
|