std::unique_ptr

This is a discussion on std::unique_ptr within the C++ Programming forums, part of the General Programming Boards category; What is the next smartest pointer when you dont have a C++0x compiler and can't use unique_ptr?...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    std::unique_ptr

    What is the next smartest pointer when you dont have a C++0x compiler and can't use unique_ptr?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Why do you need a (smart) pointer?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    I have implemented a state machine where there is one abstract State class and there is an unknown number of concrete State subclasses.

    The State machine has an STL container of State objects which can only be (smart) pointers. We cannot use abstract classes by value in a container.

    std::map<int, ??State??>

    On top of that I created a StateFactory class which provides the only means to create concrete State's. This reduces coupling to a minimum and I can alter the state machine at any time. But I dont want the factory to return raw pointers because of the risk of memory leaks then. I wanted unique_ptr so that client programmers are aware of what they are doing but I cant use it as I dont have a C++0x compiler yet.
    So whats the next best smart pointer to use?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    std::tr1::shared_ptr<State> might be appropriate. That said, as some people pointed out, you could still get the factory to return "raw" pointers, which are immediately handed off to a smart pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by laserlight View Post
    std::tr1::shared_ptr<State> might be appropriate. That said, as some people pointed out, you could still get the factory to return "raw" pointers, which are immediately handed off to a smart pointer.
    yeah but Im pretty sure this will lead to memory leaks someday as the programmers who will be using the factory are mostly experienced C programmers who just switched to C++ and are not too fond of using classes for a pointer. So Id rather not use raw pointers and just make it so that they have to use smart pointers. shared_ptr it is from now on, until I can use unique_ptr

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21