answersLogoWhite

0

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class.

class foo {

friend void bar(foo&);

int m_data;

};

void bar(foo& f)

{

f.m_data=42;

}

In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function.

Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

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

Something like this:


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

yes,we can make function inline


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++.


What is the trigonometric function for cotA plus B plus C?

cot(A+B+C) is, itself, a trigonometric function, so the question does not really make any sense!


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.


What is the difference between C plus plus and the original language?

C++ is easier to use as you have to learn slightly less and script slightly to make your function(s) work.