Question 14
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <memory>
struct Job : std::enable_shared_from_this<Job> {
std::shared_ptr<Job> self() { return shared_from_this(); }
};
int main() {
auto j = std::make_shared<Job>();
auto a = j->self();
auto b = a->self();
std::cout << j.use_count() << ' ' << (a == j) << (b == j) << '\n';
return 0;
}
- A 3 11 Correct
- B 1 11
- C 3 00
- D 2 11
Correct Answer: Option A
shared_from_this() joins the existing owner group, so the count grows to 3 and all pointers are equal.
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!