Thread: Static vector of shared_ptr

  1. #31
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by King Mir View Post
    That doesn't help you move actors in the grid.
    But a move is not a copy correct? If I move a unique_ptr from one location to another inside a container, that does not entail copy right?

    If this will never work I am happy to use auto_ptr for this example even though their copy is messed up in comparison.

  2. #32
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Moving a pointer on the grid would ideally look something like this:

    Code:
    grid[move_to_x][move_to_y] = grid[move_from_x][move_from_y];
    grid[move_from_x][move_from_y] = 0;
    That's how it'd look with dumb pointers. But, you can't do that with unique_ptrs, because the correct assignment operator is not defined. Shared pointers would actually work. auto_ptr is a perfect fit though, because auto_ptr will do both steps in the assignment; to move an auto_ptr you'd just do this:
    Code:
    grid[move_to_x][move_to_y] = grid[move_from_x][move_from_y];
    That also illustrates why auto_ptr is weird and bad in general. That's not behavior you'd expect from a normal pointer. But it is what you want here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #33
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by King Mir View Post
    Moving a pointer on the grid would ideally look something like this:

    Code:
    grid[move_to_x][move_to_y] = grid[move_from_x][move_from_y];
    grid[move_from_x][move_from_y] = 0;
    That's how it'd look with dumb pointers. But, you can't do that with unique_ptrs, because the correct assignment operator is not defined. Shared pointers would actually work. auto_ptr is a perfect fit though, because auto_ptr will do both steps in the assignment; to move an auto_ptr you'd just do this:
    Code:
    grid[move_to_x][move_to_y] = grid[move_from_x][move_from_y];
    That also illustrates why auto_ptr is weird and bad in general. That's not behavior you'd expect from a normal pointer. But it is what you want here.
    Very nice and I appreciate that perspective. By your post, I would say auto_ptr still has use when applicable and I will try all the solutions given to this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::tr1::shared_ptr question
    By StainedBlue in forum C++ Programming
    Replies: 13
    Last Post: 07-18-2010, 11:48 AM
  2. Replies: 8
    Last Post: 12-30-2008, 02:20 PM
  3. When to use shared_ptr?
    By hpesoj in forum C++ Programming
    Replies: 15
    Last Post: 07-22-2008, 04:33 AM
  4. static vector<...> in class problem.
    By alkis_y3k in forum C++ Programming
    Replies: 5
    Last Post: 01-29-2003, 04:13 PM
  5. vector static abstract data structure in C?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 05:02 PM