Which is better: storing objects vs storing pointers?
This question already has an answer here:
It depends on how you're using your store. Storing objects means you will copy them when calling Add
, and also when copying the store (among other circumstances): that could have a cost, and can lead to unwanted behaviours.
Pointers may be a good alternative, but you should then prefer managed pointers, like std::unique_ptr
, so you won't have to handle deletion.
version C:
auto e = std::unique_ptr<E>(new E());
store.Add(e); //This time the store contains managed pointers.
You can also use std::make_unique
if you have C++14 available.
version C bis:
auto e = std::make_unique<E>();
store.Add(e); //This time the store contains managed pointers.
Another option if you need to share the pointed objects can be to use std::shared_ptr
, but use it only if need it.
version D:
auto e = std::make_shared<E>();
store.Add(e); //This time the store contains shared managed pointers.
With c++11 an onward you can also just construct them directly inside the container:
std::vector<Widget> widgets;
widgets.emplace_back(/*Parameters to construct a widget*/);
Which is better and why?
That depends on your application. If the container is supposed to own the objects, and they aren't too expensive to copy, value semantics are easier. If they are expansive to copy, but easily movable, the standard containers will move them instead (you of course have to supply the move constructors).
You can also have the best of both worlds, by storing smart pointers instead. That way you'd get polymorphism if that's a requirement.
std::vector<std::unique_ptr<Widget>> widgets;
widgets.push_back(std::make_unique<Widget>(/*Parameters to construct a widget*/));
Which can cause more mistakes and which is more efficient?
The first question depends entirely on your skill as a programmer, and the second cannot be answered with a blanket statement. Programs need to be bench-marked and profiled for efficiency.
链接地址: http://www.djcxy.com/p/31642.html上一篇: 为什么在boost中使用范围指针
下一篇: 哪个更好:存储对象vs存储指针?