Which combination implements the Meyers singleton?
The function-local static gives lazy, thread-safe construction and automatic destruction at program end.
Static and Friend Members · Exercise 1 of 4
Twenty multiple-choice questions on static data members and functions, inline static, statics in inheritance and templates, singletons, friend functions, friend classes, friend operators and the rules of friendship. Pick an option to check your answer instantly.
Which combination implements the Meyers singleton?
The function-local static gives lazy, thread-safe construction and automatic destruction at program end.
What is the "static initialization order fiasco"?
Replacing such globals with functions that return function-local statics (construct on first use) avoids it.
Which statement about a friend function of class X is true?
It has no this and is called like an ordinary function, e.g. distance(a, b), not a.distance(b).
Does it matter whether a friend declaration is placed in the public, protected or private section?
Access specifiers apply to members; a friend is not a member.
Class A declares friend class B;. Can member functions of A access B's private members?
Friendship is granted one way by the class that declares it.
B is a friend of A, and C derives from B. Can C's member functions access A's private members?
Each class that needs access must be named explicitly by A.
Money has an implicit constructor from int. Why is friend Money operator+(const Money&, const Money&) better than a member operator+?
For a member operator the left operand must already be a Money – no conversion is applied to it.
Why is operator<< for printing your class written as a non-member (often a friend)?
Making it a friend lets it read private members without public getters.
What is required before writing friend void Mechanic::tune(Engine&); inside class Engine?
The compiler must know that Mechanic::tune exists. Befriending a whole class (friend class Mechanic;) needs only a declaration.
A friend function is defined inside the class body (a "hidden friend"). How is it found?
Not being visible to ordinary lookup keeps overload resolution fast and avoids accidental implicit conversions.
Timed online tests with instant scores and explanations.