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
unique_ptr doubt
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.
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.
Obrigado Álvaro.