Write a Point class that counts constructions, copies and moves, then add points to a reserved vector with push_back of a named object, push_back of a temporary, push_back with std::move, and emplace_back; print the counts.
named point: made 1 copied 0 moved 0
push_back(p): made 0 copied 1 moved 0
push_back(Point(3,4)): made 1 copied 0 moved 1
push_back(std::move(p)): made 0 copied 0 moved 1
emplace_back(5,6): made 1 copied 0 moved 0
emplace_back is the only way that constructs exactly once. (For a type of two doubles, copy and move cost the same – the point is to see which constructors run.)
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!