If a function is friend of a class, which one of the following is wrong ?

2016

If a function is friend of a class, which one of the following is wrong ?

  1. A.

    A function can only be declared a friend by a class itself.

  2. B.

    Friend functions are not members of a class, they are associated with it.

  3. C.

    Friend functions are members of a class.

  4. D.

    It can have access to all members of the class, even private ones.

Attempted by 123 students.

Show answer & explanation

Correct answer: C

Correct answer: The statement "Friend functions are members of a class." is wrong.

Why this is wrong:

  • Friend functions are not members of the class. They are ordinary functions (or functions from another class) that the class has granted special access to via a friend declaration.

  • A class grants friendship by declaring a function with the friend keyword inside its definition; the class, not the function, declares the friendship.

  • Friend functions can access private and protected members, but they do not have an implicit this pointer and are not invoked as member functions.

  • Therefore, saying a friend function is a member of the class is incorrect; it is associated with the class by permission to access its internals, not by membership.

Key takeaway: Use the friend keyword inside a class to grant access, but remember that friendship does not make the function a class member.

Explore the full course: Mppsc Assistant Professor