Thread: std::vector::erase is screwing with my pointers...

  1. #16
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    He probably does not need to worry about the copy constructor or copy assignment operator since his object contains a COM object that cannot be copied. The only way to copy a texture object is to re-create another IDirect3DTexture9 object via the data from the original or use the same file as the original. This is not really what I would call a copy. If you try to somehow copy the IDirect3DTexture9 via some other means it will most certainly crash and will also leave COM in an unknown state as well as the device. It could in extreme case cause good Direct3D code to fail b/c the device has been left in a bad state. We have all seen this when one of your fav games CTD's and then won't come back up unless you reboot.

    You might want to utilize this template class I wrote that can manage your objects in a much simpler way without all the hassles of vector. I have a templated version of this for vector as well that does the same thing without the constraint that you must specifiy the size (IE: the main advantage of vector over plain old arrays) but there are some unresolved issues with it.

    I use this class for my asteroids clone and I think it will do just what you want. I will also attach it's associated unit test so you can see how to use it. You will see in the unit test it creates an array of integers and then precedes to remove the item at index 0. It then takes the last item in the array and places it at item 0 and then removes the last item in the array by reducing the size of the array by 1. The array always shrinks from the end. No items are physically removed from the middle of the array. This ensures that arrays do not have 'holes' in them so they can be iterated without having to check any pointers or the validity of indices. If the array's current size is 10 you can be assured that all 10 elements are 100% valid. When it moves the last element to the removal index it notifies the listener about the event so it can then make adjustments. The idea is that this class would be used inside of a manager that could then notify the correct object that their ID has changed and would tell it what the new ID is. So rather than have a listener for each element I opted to have one listener for the entire array which I envisioned would be the class utilizing the array.

    The only other structure that will do exactly what you what is std::map but it has O(n log n) access time and the array has O(1).
    Last edited by VirtualAce; 03-12-2011 at 11:41 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Bubba
    He probably does not need to worry about the copy constructor or copy assignment operator since his object contains a COM object that cannot be copied. The only way to copy a texture object is to re-create another IDirect3DTexture9 object via the data from the original or use the same file as the original. This is not really what I would call a copy.
    Agreed. It would thus be better to disable the copy constructor and copy assignment operator instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Agreed. It would thus be better to disable the copy constructor and copy assignment operator instead.
    Agreed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2008, 10:10 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM