| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Lars Tackmann" |
| Date: |
27 Nov 2003 06:52:00 AM |
| Object: |
Destructor and print visitor |
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.
.
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: Destructor and print visitor |
27 Nov 2003 08:20:08 AM |
|
|
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
kbuchegg@gascad.at
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Destructor and print visitor |
27 Nov 2003 11:41:09 AM |
|
|
"Lars Tackmann" <roland@diku.dk> wrote...
I have attaced [...]
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
.
|
|
|
|
|

|
Related Articles |
|
|