unique_ptr doubt

Started by
7 comments, last by nunobarao 9 years, 1 month ago

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



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.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

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.


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.

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).

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.

(or implement make_unique yourself; it's not terribly hard).


Or use the implementation from N3656. It won't work with VS2012 in which case you could use an implementation posted on StackOverflow which is compatible.
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.

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.

This topic is closed to new replies.

Advertisement