pointer problem

T

Thomas Baier

Hi there,

I've written a list template class and a tree template class. The list class
contains some sort algorithms and I have to sort a list with elements of
type tree<T>*. So I need to compare these elements. I've overloaded the
<,>,<=,>= operators of the tree class, but of course that doesn't work
cause I don't have to compare elements of type tree<T>, but elements of
type tree<T>*. So if I would make

tree<int> a(3);
tree<int> b(4);
cout << a<b;
everything works fine. But I have to do something like

tree<int>* a = new tree<int>(3);
tree<int>* a = new tree<int>(4);
cout << a<b;

I know that this would just compare the adresses of both objects, but I
don't know how to solve that, cause at running time "I" do not know whether
the elements of my list are pointers or not. So I cannot write
*a<*b
cause it could be that a and b aren't pointers.

Is there any way to solve that problem (a way to find out whether there were
pointers or not or something like that)



Thanks for any help

Thomas
 
L

lilburne

Thomas said:
Is there any way to solve that problem (a way to find out whether there were
pointers or not or something like that)

Surely you either have pointers or objects, the types of the
object?
 
R

Rob Williscroft

Thomas Baier wrote in
Hi there,

I've written a list template class and a tree template class. The list
class contains some sort algorithms and I have to sort a list with
elements of type tree<T>*. So I need to compare these elements. I've
overloaded the <,>,<=,>= operators of the tree class, but of course
that doesn't work cause I don't have to compare elements of type
tree<T>, but elements of type tree<T>*. So if I would make

tree<int> a(3);
tree<int> b(4);
cout << a<b;
everything works fine. But I have to do something like

tree<int>* a = new tree<int>(3);
tree<int>* a = new tree<int>(4);
cout << a<b;

I know that this would just compare the adresses of both objects, but
I don't know how to solve that, cause at running time "I" do not know
whether the elements of my list are pointers or not. So I cannot write
*a<*b
cause it could be that a and b aren't pointers.

Is there any way to solve that problem (a way to find out whether
there were pointers or not or something like that)

#include <iostream>
#include <iomanip>

template < typename T >
struct less_helper
{
static bool apply( T const &lhs, T const & rhs )
{
return lhs < rhs;
}
};

// Now specialize for pointer:

template < typename T >
struct less_helper< T * >
{
static bool apply( T const *lhs, T const *rhs )
{
return *lhs < *rhs;
}
};

//use:

template < typename T >
bool example_is_less( T const &lhs, T const &rhs )
{
return less_helper< T >::apply( lhs, rhs );
}

int main()
{
using namespace std;

int a = 1, b = 2;

cerr << boolalpha;

cerr << example_is_less( a, b ) << "\n";
cerr << example_is_less( a, a ) << "\n";
cerr << example_is_less( b, a ) << "\n";

cerr << example_is_less( &a, &b ) << "\n";
cerr << example_is_less( &a, &a ) << "\n";
cerr << example_is_less( &b, &a ) << "\n";

// check it isn't address dependant:
b = 1, a = 2;

cerr << example_is_less( &b, &a ) << "\n";
cerr << example_is_less( &b, &b ) << "\n";
cerr << example_is_less( &a, &b ) << "\n";
}

HTH

Rob.
 
T

Thomas Baier

Works fine, but is there a way to overload the <-operator so that
a<b is the same as &a<&b?
 
R

Rob Williscroft

Thomas Baier wrote in online.net:
Works fine, but is there a way to overload the <-operator so that
a<b is the same as &a<&b?

No, at least one argument to any user defined operator must be a
user defined type (struct, class or union).

Rob.
 
T

Thomas Baier

Hm, maybe I don't really understood what you mean, but I want to overload
the operator of my user defined class (tree). So I've written something
like:

//list.h
template <typename T> class list {
protected:
//...
T* content;

public:
//...
void sort() {
//...
if(content<content[i+1])
//...
}
};

//tree.h
#include "list.h"
template <typename T> class tree {
protected:
int key;
//...
public:
//...
list<tree<T>*> traverse(/*...*/); /*returns a list with all nodes of the
tree (which are of cause also trees)*/
void balance() {
list<tree<T>*> l = traverse();
l.sort(); //returns trash as it only compares the addresses

//...create new balanced tree out of the sortet list

}
bool operator<(tree<T> operand) {
return this->key<operand.key;
}
//...
};


So do anybody now see a way to solve that problem?
 
T

Thomas Baier

or is there maybe a way by making a listelement class in the list class and
overload the operators of that class?
 
T

Thomas Baier

I really recognized your original response, but I thought it would be nice
if I could do it with <,>,...
So if there's no way to do it, I'll do it like your first response told me.

Thanks a lot for your help

Thomas

Rob said:
Thomas Baier wrote in

Top Post rearranged ..
Hm, maybe I don't really understood what you mean, but I want to
overload the operator of my user defined class (tree). So I've written
something like:

Word games will not help. A pointer, be it to an inbuilt type or a UDT,
is an not a user defined type (UDT).
//list.h
template <typename T> class list {
protected:
//...
T* content;

public:
//...
void sort() {
//...
if(content<content[i+1])


This is where you need to make the change, I showed the basic's of how
in my original response.

Eg:

if ( example_is_less said:
//...
}
};

[snip]


So do anybody now see a way to solve that problem?

See the my original response.

Rob.
 
R

Rob Williscroft

Thomas Baier wrote in

Top Post rearranged ..
Hm, maybe I don't really understood what you mean, but I want to
overload the operator of my user defined class (tree). So I've written
something like:

Word games will not help. A pointer, be it to an inbuilt type or a UDT,
is an not a user defined type (UDT).
//list.h
template <typename T> class list {
protected:
//...
T* content;

public:
//...
void sort() {
//...
if(content<content[i+1])


This is where you need to make the change, I showed the basic's of how
in my original response.

Eg:

if ( example_is_less said:
//...
}
};

[snip]


So do anybody now see a way to solve that problem?

See the my original response.

Rob.
 

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

Forum statistics

Threads
474,145
Messages
2,570,827
Members
47,373
Latest member
Desiree036

Latest Threads

Top