ProgrammingHardProduct-company placement · C++
Two classes hold shared_ptr members pointing to each other (a parent owns its child and the child owns its parent). What happens when the last outside shared_ptr to them is destroyed?
- A. Neither object is destroyed: each keeps the other's reference count above zero (a leak); one link should be a weak_ptr
- B. Both are destroyed, child first
- C. Both are destroyed, parent first
- D. std::terminate is called because of the cycle
Correct answer: A. Neither object is destroyed: each keeps the other's reference count above zero (a leak); one link should be a weak_ptr
Reference counting cannot reclaim cycles. Making the back-pointer a weak_ptr breaks the ownership cycle while still allowing access via lock().
More OOP with C++ and Java questions
- OrderService creates new MySqlRepository() inside its own constructor. It is refactored to receive a Repository interface through its…
- An interface Machine declares print(), scan() and fax(). A SimplePrinter class implements it and has to make scan() and fax() throw…
- A GUI toolkit must create a matching Button, Checkbox and Window for either a Windows or a Mac theme, guaranteeing that all widgets come…
- In C++11 and later, which statement about this 'Meyers singleton' is correct? cpp Logger& instance() { static Logger log; return log; }
- How many of these are resolved at COMPILE time in C++? (i) Which function body a call to a non-virtual member function through a…
- What is printed? java String b = "java"; String a = "ja" + "va"; String c = "ja"; String d = c + "va"; final String e = "ja"; String f = e…