I've started using std::tr1::shared_ptr and so far I'm quite liking it. I understand some of the pitfalls (e.g. two classing containing smart pointer members to each other). But there are other cases which I'm unsure about whether to use smart pointer or not.
E.g.
class Scene;
typedef shared_ptr<Scene> ScenePtr;
Class SceneManager {
public:
int size() { return _scenes.size(); }
ScenePtr scene(int i) { return _scenes[i]; }
private:
vector<ScenePtr> _scenes;
}
This is all good and working nicely. However, if I have an external controller, is there any disadvantage of doing:
for(int i=0; i<_sceneManager->size(); i++) {
ScenePtr scene = _sceneManager->scene(i);
scene->doSomething();
}
here 'scene' will obviously increase the reference count of each ScenePtr, but then decrease it again when it goes out of scope. But is there any performance (or any other) disadvantage?
Alternatively I could use a normal C pointer
for(int i=0; i<_sceneManager->size(); i++) {
Scene* scene = _sceneManager->scene(i).get();
scene->doSomething();
}
But is this actually any better? or is it identical? Does the reference count get increased? (on the function call), but then decreased as soon as it leaves the first line?
I often used to use a reference, would that have any complications?
for(int i=0; i<_sceneManager->size(); i++) {
Scene& scene = *_sceneManager->scene(i);
scene.doSomething();
}
And finally, is it even a good idea to return a ScenePtr at all, or should SceneManager::scene(i) return a (Scene*) or even (Scene&)?
Similarly, to do an ordered map I often used to use: vector _scenesArray; map _scenesMap;
So I can access the objects by name, or in order. Using std::tr1::shared_ptr should they both be ScenePtr? or just one of them ScenePtr and the other one Scene*?