Destructor and print visitor



 DEVELOP > c-Plus-Plus > Destructor and print visitor

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER