Declaring a Member Function as a Friend of Another Class

Declaring a Member Function as a Friend of Another Class

Although you can declare an entire class a friend of another class, like this:

 class A{ friend class B; //B has  access  to every member of A //..};

However, in most cases, only one or two member functions of B need such unrestricted access to A. To avoid indiscriminate access, you can declare individual member functions of B as friends. To declare a member function as a friend, simply provide its prototype followed by the keyword friend. For example:

 class B; // fwd declaration required; A::f() takes B&struct A{ int f(B&);};struct B{ friend int A::f(B&); // a member function friendprivate: int x;};int A::f(B& b) { return b.x; // access private member of b}int main(){ A a; B b; a.f(b); // access a private member of b}
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several