Difficulty: Easy
Correct Answer: Object
Explanation:
Introduction:
Friendship is a targeted access-control mechanism that allows external code to access a class's non-public members. This question asks which kinds of entities can be granted friendship and which cannot.
Given Data / Assumptions:
Concept / Approach:
In C++, you can declare as friend: specific non-member functions, all member functions of another class by befriending that class, and operator overload functions (which are just functions). Friendship is granted to functions or types, not to individual objects/instances. Therefore, an “object” cannot be declared a friend.
Step-by-Step Solution:
1) Consider friend void helper(MyClass&); → legal: function is a friend.2) Consider friend class Logger; → legal: all members of Logger have access.3) Consider friend std::ostream& operator<<(std::ostream&, const MyClass&); → legal operator function.4) Attempt “friend someObject;” → ill-formed; objects cannot be befriended.
Verification / Alternative check:
Try compiling a friend declaration with a specific object variable; compilers reject it because friend expects a type or a function declaration.
Why Other Options Are Wrong:
Function: valid friend target.Class: valid; grants access to all its members.Operator function: valid because operators are functions.
Common Pitfalls:
Confusing granting friendship to a single member function versus an entire class; remember that friend declarations are not transitive or inherited—each friendship must be explicitly declared.
Final Answer:
Object
Discussion & Comments