jeudi 4 juin 2020

Why is it a problem to get the return of unique_ptr before assigning to a member variable?

Why are the results different and the problem occurs in the second method?

1) No error.

auto person = getPerson(); // return type : std::unique_ptr<Person>
static_cast<Student*>(person.get())->foo(); // Student inherits Person

2) Segmentation fault before entering the function foo()

auto person = getPerson().get();
static_cast<Student*>(person)->foo(); 

Aucun commentaire:

Enregistrer un commentaire