answersLogoWhite

0

Friend functions are required whenever you have a function that cannot logically be declared a member of the class, but that still requires private access to the class members. In most cases, private access (and therefore protected access) to the class members would be undesirable in an external function; members are declared private or protected for a reason. However, if the class interface is such that the external function cannot fulfil its role and the interface cannot be modified because it would undermine the encapsulation, then the class may declare the function to be a friend.

The function itself has no access to the implicit thispointer, thus it must accept an instance of the class by reference (where an instance is guaranteed to exist for every call) or by pointer (if an instance may or may not exist). The function can then indirectly access the private members of the class via the reference or non-NULL pointer.

Friends functions are often employed whenever two (or more) classes must work closely together in such a way that an operation upon any one instance of a class must automatically invoke private or protected operations upon one or more of the others. For instance, in a parent/child structure, the parent often contains a private embedded list of its children while each child contains an embedded read-only pointer to its parent. Thus when adding a child to a parent, it would be desirable for the parent::add_child(child&) method to automatically modify the private child::m_parent member of the given child. Therefore the parent::add_child() method should be declared a friend of the child class.

User Avatar

Wiki User

∙ 13y ago
User Avatar

What else can I help you with?

Related Questions

What is the code to perform friend function in C plus plus?

Something like this:


What is the operator of power in c plus plus?

There is no "power" operator in C or C++. You need to the use the math library function pow().


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


How do you correct the C plus plus programming error missing function header?

You need to #include the header file that contains the missing function's declaration.


What are the limitations of friend function in c plus plus?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline