Destructor and print visitor

L

Lars Tackmann

Hi

I have a problem where i have allocated an array on the heap, using
new in a class called "Array". In the end of my main i call a visit
method that visits all of the elements in class "Array" using a
visitor object

eg.

// visits each element in the list
template <class T>
void Array<T>::visit(Visitor<T> aVisitor)
{
// create an iterator object
ArrayIterator<T> iter(*this);

// start visit
aVisitor.startVisit();

// do visit
for(iter.begin(); !iter.atEnd(); ++iter)
aVisitor.doVisit(*iter);

// end visit
aVisitor.endVisit();
}

I have a visitor that prints each element using cout. If i use this
visitor it will print garbage if i have a destructor in the Array class.
If i delete the destructor in the Array class then it all works fine
(except that i do not free my memory). I figure that i need to delay the
call of the destructor in the Array class or i need to ensure that my
program do not continue while my visitor prints each elements using cout.


How do i ensure that my data in the array class is not deleted from the
heap before i have used it.

Thanks.
 
K

Karl Heinz Buchegger

Lars said:
How do i ensure that my data in the array class is not deleted from the
heap before i have used it.

By adjusting the scopes such that this will not happen.
Please post a short, complete, compilable program which demonstrates
your problem.
Your problem description sounds as if there is more to the *real* problem
then you have already recognized.
 

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,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top