Topic: destructor called by stl's vector::resize()
Author: "Chris Hines" <chrishines@email.msn.com>
Date: 1999/04/20 Raw View
Write a copy constructor for your class that prints a message when it is
called and see how that changes your test.
When you call vector<>::resize(), the container may have to reallocate its
memory and copy the existing elements to the new area, after which it will
destroy the original elements.
--
Chris Hines
The sooner you start coding, the longer it will take you to finish.
Hwee Kuan Lee <hwee+@andrew.cmu.edu> wrote in message
news:or72BLVz000143wp5d@andrew.cmu.edu...
> > i found that when the resize function of a vector class is called, the
> > destructor of the vector's element type is also called. example:
> >
> > class X {
> > public:
> > X() {
> > cout<<"construct"<<endl;
> > }
> > ~X() {
> > cout<<"destruct"<<endl;
> > }
> > };
> >
> > main () {
> >
> > vector<X> vx;
> >
> > vx.resize(2);
> >
> > cout<<"----- end program "<<endl;
> >
> > }
> >
> > this program when compiled with egcs-g++ on linux red hat will yield,
> >
> > construct
> > destruct
> > ----- end program
> > destruct
> > destruct
> >
> > i have a few questions regarding this output,
> >
> > first, why is the destructor called when resize function is called?
> > next is that why is the constructor called once, while destructor
> > is called three times? construct once, but destroy three times?
> >
> >
> > thanks.
> >
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hwee Kuan Lee <hwee+@andrew.cmu.edu>
Date: 1999/04/20 Raw View
> i found that when the resize function of a vector class is called, the
> destructor of the vector's element type is also called. example:
>
> class X {
> public:
> X() {
> cout<<"construct"<<endl;
> }
> ~X() {
> cout<<"destruct"<<endl;
> }
> };
>
> main () {
>
> vector<X> vx;
>
> vx.resize(2);
>
> cout<<"----- end program "<<endl;
>
> }
>
> this program when compiled with egcs-g++ on linux red hat will yield,
>
> construct
> destruct
> ----- end program
> destruct
> destruct
>
> i have a few questions regarding this output,
>
> first, why is the destructor called when resize function is called?
> next is that why is the constructor called once, while destructor
> is called three times? construct once, but destroy three times?
>
>
> thanks.
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]