CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340

    STL deque question

    Hello all,

    I thought when I call erase() method on an STL deque, it is supposed to call the corresponding destructor of the object. However, it does not when I do this. Here is some code to illustrate the problem.

    std:eque<Wave *> _wavePool;
    std:eque<Wave *>::iterator it;

    Wave *wav1 = new Wave("D:\\waves\\v1.wav", true);
    Wave *wav2 = new Wave("D:\\waves\\think.wav", true);
    _wavePool.push_back(wav1);
    _wavePool.push_back(wav2);
    it = _wavePool.begin()

    Now, if I call...

    _wavePool.erase(it);

    The wave destructor never gets called. Am I not supposed to put heap pointers in a queue or do I have to do this manually.

    Thanks for any help you might give me.

    Xargon

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    In case of having any container filled with pointers, the container only destroys its copy of the pointer itself but not the object it is pointing to. You have to do this manually...thus
    Code:
    std::deque<Wave *> _wavePool;
    std::deque<Wave *>::iterator it;
    
    Wave *wav1 = new Wave("D:\\waves\\v1.wav", true);
    Wave *wav2 = new Wave("D:\\waves\\think.wav", true);
    _wavePool.push_back(wav1);
    _wavePool.push_back(wav2);
    it = _wavePool.begin();
    
    ...
    
    // First delete object
    delete *it;
    
    // Then delete it from container
    _wavePool.erase(it);

  3. #3
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    Ahhhhhh, I get it now. Thanks man!

    Cheers,

    Xargon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured