By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Destructor and print visitor

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Lars Tackmann wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a

This discussion thread is closed

Replies have been disabled for this discussion.