templates and operators

M

Michele

Hello everybody,

I don't have clear in my mind how to use templates and overloaded
operator with them...
I hope someone may give me an hint...

I use a library aTemplate which has defined:
AddFirst(), First(), Last() etc,
but it doesn't have a function to access to the element.
I have to use an iterator aTemplateIterator which has defined:
Set(), ..., and:
operator++()
operator T*()

my code is the following:

class myElement {
public:
int key;
int value;
};

class myTable {
public:
myElement item;
...
}

class myObjectData {
public:
aTemplate<myTable> header;
.....
}

When I try to access to the element by using the operator I have problems.

myObjectData* info;
aTemplateIterator<myTable> aIter(info->header);

aIter.First(); //First() is a method of aTemplate

while (aIter != NULL) {

int i = *aIter.item.key; //here stands the error

aIter.operator++(1); //this is fine
}

Error is: "item is not a member of aTemplateIterator<myTable>"

QUESTION: How can I access to item.key?

thanks to anyone who could give me an answer...
michele
 
T

tom_usenet

Hello everybody,

I don't have clear in my mind how to use templates and overloaded
operator with them...
I hope someone may give me an hint...

I use a library aTemplate which has defined:
AddFirst(), First(), Last() etc,
but it doesn't have a function to access to the element.
I have to use an iterator aTemplateIterator which has defined:
Set(), ..., and:
operator++()
operator T*()

Iterators shouldn't define an operator T*. They should define at
least:

operator++
operator++(int) usually
operator*
operator->
operator==
operator!=
my code is the following:

class myElement {
public:
int key;
int value;
};

class myTable {
public:
myElement item;
...
}

class myObjectData {
public:
aTemplate<myTable> header;
....
}

When I try to access to the element by using the operator I have problems.

myObjectData* info;
aTemplateIterator<myTable> aIter(info->header);

aIter.First(); //First() is a method of aTemplate

while (aIter != NULL) {

int i = *aIter.item.key; //here stands the error

You want aIter->item.key;
or
(*aIter).item.key;
aIter.operator++(1); //this is fine

Surely
++aIter;

Tom
 
M

Michele

Thank you Tom,

things are becoming clearer.
Iterators shouldn't define an operator T*.

mh... why?

You want aIter->item.key;
or
(*aIter).item.key;

You are right.
But.... _DUMMY QUESTION_ I don't understand the difference between your
second line and the one I wrote.
I see parenthesys ok, but I don't understand why it should non work
without them.
In this line:
*aIter.item.key;
the compiler is not supposed to evaluate *aIter first and after follows
the dot and other elements?


Thank you anyway for your help.

michele
 
T

tom_usenet

Thank you Tom,

things are becoming clearer.


mh... why?

Becuase it is potentially dangerous - you can use pointer arithmetic
on the T*, which you want to avoid. e.g.
MyIt += 10;
will compile, but have unexpected results.
You are right.
But.... _DUMMY QUESTION_ I don't understand the difference between your
second line and the one I wrote.
I see parenthesys ok, but I don't understand why it should non work
without them.
In this line:
*aIter.item.key;
the compiler is not supposed to evaluate *aIter first and after follows
the dot and other elements?

*aIter.item.key
is equivalent to
*(aIter.item.key)
since . has higher precedence than *

Tom
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top