The code is not really a good example as you shouldn't use new any more due to the problems outlined. Your code can be written this way:
new
int GetCost(request) { Checker checker = Checker(request); int cost = 0; if (!checker.Important) return 20; if (!checker.HasDiscount) return 14; if (!checker.IsMember) return 12; return 10; }
If you want to use pointers, you should always wrap them in a smart pointer, e.g. std::make_shared(new Checker). That way they will also be removed when the scope is exited
std::make_shared(new Checker)
Thanks for the feedback,
Smart pointers make me feel dumb, but they look like a great tool. I have a lot to learn about modern c++...
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The code is not really a good example as you shouldn't use
new
any more due to the problems outlined. Your code can be written this way:If you want to use pointers, you should always wrap them in a smart pointer, e.g.
std::make_shared(new Checker)
. That way they will also be removed when the scope is exitedThanks for the feedback,
Smart pointers make me feel dumb, but they look like a great tool. I have a lot to learn about modern c++...