Jump to content

is it necessary to take case of “delete item” if the item is create by XXX::type_id::create instead of new?


Recommended Posts

You need to take care yourself as the factory just calls new and returns a raw pointer. You should use a std::unique_ptr to hold the raw pointer:

#include <memory>

std::unique_ptr<A> a{A::type_id::create("item", this)}; 

or if you have to keep declaration and initialization separate:
 

#include <memory>

std::unique_ptr<A> a;
a.reset(A::type_id::create("item", this)); 

This way C++ takes care of destruction properly

Link to comment
Share on other sites

your reply is very helpful.

 

1 hour ago, Eyck said:

You need to take care yourself as the factory just calls new and returns a raw pointer. You should use a std::unique_ptr to hold the raw pointer:

#include <memory>

std::unique_ptr<A> a{A::type_id::create("item", this)}; 

or if you have to keep declaration and initialization separate:
 

#include <memory>

std::unique_ptr<A> a;
a.reset(A::type_id::create("item", this)); 

This way C++ takes care of destruction properly

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...