containers - Writing c++ without new and delete keyword? Best approach? -
i'm trying write c++ code without using keywords new
, delete
.
first, practice or not ?
one of side effects of coding can't rely on nullptr
for empty values.
for instance:
std::array<std::array<fb::block *, panel::y>, panel::x> blocks;
becomes
std::array<std::array<fb::block, panel::y>, panel::x> blocks;
what best way describe empty block ?
i though using this:
class block { private: protected: public: ... static const block empty; }; } const block empty(0, blocktype::tutorial, 0, 0);
what guys think ? approach ?
if want write safe code , simplify memory management, should indeed avoid delete
. there no need discard new
. best way go allocate object want new
, store in smart pointers std::shared_ptr
or std::unique_ptr
. can compare pointers nullptr
. , objects automatically deleted.
some advise replace raw new
make_shared
, make_unique
calls in scenario.
the related question when utilize t&
, when utilize t*
passing argument function. rule of thumb is: utilize t*
whenever input argument can absent, utilize t&
ensure input argument provided.
c++ containers
No comments:
Post a Comment