Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique pointer - code example issue ? #228

Open
suhasghorp opened this issue Oct 7, 2024 · 0 comments
Open

Unique pointer - code example issue ? #228

suhasghorp opened this issue Oct 7, 2024 · 0 comments

Comments

@suhasghorp
Copy link

suhasghorp commented Oct 7, 2024

Working thru chapter 11 on Smart Pointers. I am not sure why I am getting a different num of instances than the book. I have removed the Catch2 test cases and put all code in main(). I do not get 2 instances at any point of time in the below code, contrary to what the books says. Thanks
C++20 with Visual Studio 2022

`struct DeadMenOfDunharrow {

DeadMenOfDunharrow(const char* m = "")
    : message{ m } {
    oaths_to_fulfill++;
}
~DeadMenOfDunharrow() {
    oaths_to_fulfill--;
}
const char* message;
static int oaths_to_fulfill;
};

int DeadMenOfDunharrow::oaths_to_fulfill{};

void print_number() {
    std::cout << "num of instances: " << DeadMenOfDunharrow::oaths_to_fulfill << std::endl;
}

int main()
{

auto aragorn = std::make_unique<DeadMenOfDunharrow>();
auto son_of_arathorn{ std::move(aragorn) };
print_number(); // prints 1 
son_of_arathorn = std::make_unique<DeadMenOfDunharrow>();
print_number(); // prints 1, book expects 2 here
son_of_arathorn = std::move(aragorn);
print_number(); // prints 0

}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant