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.
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.
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.
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.
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 function call.
Something like this:
There is no "power" operator in C or C++. You need to the use the math library function pow().
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.
The keyword "friend" allows a function or variable to have access to a protected member inside a class.
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.
Yes, there can be friend functions in C++.
There is no such term as "building function" in C++.
You need to #include the header file that contains the missing function's declaration.
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.
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 function call.
yes,we can make function inline