• Advertisement
Sign in to follow this  

unique_ptr doubt

This topic is 1054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, its been a while since my last post, i got some doubts with unique_ptr, and was thinking to request some help from you guys.

So, if i had to make a std::unique_ptr<ClassA> foo(new ClassA()); i´m ok with that, i know that we are instantiate a new object with a unique pointer am i correct? correct me if i´m wrong.
My problem begins when i want to create a std::unique_ptr<ClassA> foo(new ClassB()); Here i´m instantiate a new object from class B with a unique pointer to class A ??? I got confused here.

If some one can give me a hint here or show me a good article or book on this topic i would appreciate a lot.

thank you



 

Share this post


Link to post
Share on other sites
Advertisement

Use make_unique first of all, not new directly.

 

If ClassB inhertis from classA then its ok to store the pointer variable as classA. However you'll need to cast it back to ClassB to reach it's functionality.

 

Once you get used to using make_unique and make_shared you won't use new/delete directly anymore.

Edited by EddieV223

Share this post


Link to post
Share on other sites

Use make_unique first of all, not new directly.

 

If ClassB inhertis from classA then its ok to store the pointer variable as classA. However you'll need to cast it back to ClassB to reach it's functionality.

 

Once you get used to using make_unique and make_shared you won't use new/delete directly anymore.

 

Ok now that you refer inheritance, i think that was my problem, i forgot to look at inheritance.
Yes ClassB inherits from ClassA.


 

Share this post


Link to post
Share on other sites

Use make_unique first of all, not new directly.

 

If ClassB inhertis from classA then its ok to store the pointer variable as classA. However you'll need to cast it back to ClassB to reach it's functionality.

 

Once you get used to using make_unique and make_shared you won't use new/delete directly anymore.

Yes that makes more sense to me know with inheritance.

Problem solved

Thank you.

Share this post


Link to post
Share on other sites

 

Use make_unique first of all, not new directly.

 

 

However, make_unique is a C++14 thing, so if your compiler does not implement it yet, you can use new directly as you did in your examples (or implement make_unique yourself; it's not terribly hard).

 

Thank you Josh.

Share this post


Link to post
Share on other sites
Nuno, one comment that might help with your English: The correct translation for the Portuguese "dúvida" or the Spanish "duda" is "question", not "doubt". When I saw the title of the thread I knew the author's native language was either Portuguese or Spanish. I grew up speaking both and I used to make the same mistake in English.

Share this post


Link to post
Share on other sites

Nuno, one comment that might help with your English: The correct translation for the Portuguese "dúvida" or the Spanish "duda" is "question", not "doubt". When I saw the title of the thread I knew the author's native language was either Portuguese or Spanish. I grew up speaking both and I used to make the same mistake in English.

Obrigado Álvaro.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement