Thread: std::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
    28,413
    Why do you need a (smart) pointer?
    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. #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
    28,413
    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.
    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

  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