I have wired situation with my object I have created object with unique_ptr, however I wanted to full prof a bit and if someone select book that was already rented it should get message This book is already borrowed!
but when I added if else statement my object is being destroyed. If I comment it out (if/else) it will work fine.
it appears I am destroying object just by reading it. why is that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
case'3': {
std::cout << "Please select book to borrow: ";
lib.Print();
unsignedint book_selection = 0;
std::cin >> book_selection;
if (lib.Borrow(book_selection) != nullptr)
{
std::unique_ptr<Book> borrow_book_ptr = lib.Borrow(book_selection);
person.Borrow(std::move(borrow_book_ptr));
} else{
std::cout << "This book is already borrowed!\n";
}
std::system("clear");
break;
Using unique pointers can sometimes cause that kind of errors and it's important to detect and solve them as quick as possible as they mistake get harder to find. There are external tools you can use in order to do it, such as checkamrx that works pretty good.
Goosd luck.