answersLogoWhite

0


Best Answer

Within a class declaration, include the following:

friend ;

For example:

friend class MyClass; // friend class

friend void MyClass::SomeFunction(); // friend class method.

friend void MyFunction(); // friend function.

When declaring friend functions and friend class methods, you must declare an unambiguous prototype, including its return type and its parameter types, and all instances of the const keyword. If the function prototype is contained in another file, that file must also be included in the class declaration file.

Friends can be declared anywhere in a class declaration. However, friends cannot be inherited so it doesn't matter if they're declared public, private or protected (access specifiers have no meaning to friends). But be aware that friends have privileged access to all members, including private and protected members. This is said to undermine the fundamentals of encapsulation, however the purpose of a friend is to extend the class interface, and is no more an undermining of encapsulation than is public inheritance. However, access should be limited to as few friends as possible; a class with many friends implies a fundamental design flaw in the class.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How friend function in c plus plus created?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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

Something like this:


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.


What are the building function in c plus plus?

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


Can there be friend functions in c plus plus?

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


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


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.