ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
struct S {
void f() & { cout << "L"; }
void f() && { cout << "R"; }
};
int main() {
S s;
s.f();
S().f();
std::move(s).f();
}- A. LRR
- B. LLL
- C. LRL
- D. RRR
Correct answer: A. LRR
Ref-qualifiers overload member functions on the value category of the object: a named object is an lvalue (&), while a temporary and std::move(s) are rvalues (&&).
More OOP with C++ and Java questions
- What is printed? cpp int main() { int x = 0; auto f = [x]() mutable { return ++x; }; f(); f(); cout << f() << x; }
- What is printed? cpp int main() { std::function<int()> f; try { cout << f(); } catch (const badfunctioncall&) { cout << "E"; } }
- What is printed? cpp struct W { W(int, int) { cout << "2"; } W(initializerlist<int>) { cout << "L"; } }; int main() { W a(1, 2); W b{1…
- Two classes hold sharedptr members pointing to each other (a parent owns its child and the child owns its parent). What happens when the…
- 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…