What is the next smartest pointer when you dont have a C++0x compiler and can't use unique_ptr?
What is the next smartest pointer when you dont have a C++0x compiler and can't use unique_ptr?
Why do you need a (smart) pointer?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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