public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: erase operation in STL
@ 1997-08-31 23:12 Joe Buck
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Buck @ 1997-08-31 23:12 UTC (permalink / raw)
  To: egcs

> I have a problem when class vector of STL is used with egcs-970828.

Put in a message that calls the copy constructor and assignment operator
and all will become clear.

> In the following program, a vector av of two elements is created.
> Then the first element is erased.  Naturally I expect the destructor
> for the first element is called, but instead STL calls the destructor
> of the second element.  

That's because it does the erase by copying the second element to the
first and then destroying the second element.

(use the source, Luke; it is easy to read and understand the vector::erase
method's code).

> second element (now becoming the first element) is called again.

Again, this is what is supposed to happen.

> I have no access of other implementation of STL.  Could someone tell
> me if this behavior is correct?

Yes, it's correct.  Other implementations are possible, since vector is
entitled to assume that class A's basic operations (copy ctor, assignment
operator, dtor) work as they should; it can choose what order to do things
in.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: erase operation in STL
  1997-08-31 18:57 Weiwen Liu
@ 1997-08-31 21:31 ` YMP Consulting Inc.
  0 siblings, 0 replies; 3+ messages in thread
From: YMP Consulting Inc. @ 1997-08-31 21:31 UTC (permalink / raw)
  To: egcs

What is happening is 2 is being assinged to 1, and then the old
2 is destroyed.

In vectors erase causes later objects to be shifted down.

I Hope this helps.

Weiwen Liu wrote:
> 
> I have a problem when class vector of STL is used with egcs-970828.
> 
> In the following program, a vector av of two elements is created.
> Then the first element is erased.  Naturally I expect the destructor
> for the first element is called, but instead STL calls the destructor
> of the second element.  When the program exits, the destructor for the
> second element (now becoming the first element) is called again.
> 
> I have no access of other implementation of STL.  Could someone tell
> me if this behavior is correct?
> 
> Weiwen
> =====================
> #include 
> #include 
> 
> class A
> {
> public:
>     A(int n)
>             :itsA(n)
>     {}
>     ~A()
>     {cout  
> private:
>     int itsA;
> };
> 
> int main()
> {
>     vector av;
>     cout  
>     A a1(1);
>     av.push_back(a1);
>     cout      A a2(2);
>     av.push_back(a2);
>     cout  
>     cout      av.erase(av.begin());
>     cout  
>     return 0;
> }
> 
> The program outputs:
> 
> destroy1
> begin erase: destroy2 end erase
> destroy2 destroy1 destroy2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* erase operation in STL
@ 1997-08-31 18:57 Weiwen Liu
  1997-08-31 21:31 ` YMP Consulting Inc.
  0 siblings, 1 reply; 3+ messages in thread
From: Weiwen Liu @ 1997-08-31 18:57 UTC (permalink / raw)
  To: egcs

I have a problem when class vector of STL is used with egcs-970828.

In the following program, a vector av of two elements is created.
Then the first element is erased.  Naturally I expect the destructor
for the first element is called, but instead STL calls the destructor
of the second element.  When the program exits, the destructor for the
second element (now becoming the first element) is called again.

I have no access of other implementation of STL.  Could someone tell
me if this behavior is correct?

Weiwen
=====================
#include 
#include 

class A
{
public:
    A(int n)
            :itsA(n)
    {}
    ~A()
    {cout  av;
    cout 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-08-31 23:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-08-31 23:12 erase operation in STL Joe Buck
  -- strict thread matches above, loose matches on Subject: below --
1997-08-31 18:57 Weiwen Liu
1997-08-31 21:31 ` YMP Consulting Inc.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).