To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Friend function

From Wikipedia, the free encyclopedia

In object-oriented programming, a friend function, that is a "friend" of a given class, is a function that is given the same access as methods to private and protected data.[1]

A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods. Friend functions allow alternative syntax to use objects, for instance f(x) instead of x.f(), or g(x,y) instead of x.g(y). Friend functions have the same implications on encapsulation as methods.

A similar concept is that of friend class.

YouTube Encyclopedic

  • 1/3
    Views:
    86 191
    196 404
    48 430
  • Friend Function In C++ (HINDI/URDU)
  • Buckys C++ Programming Tutorials - 48 - friend
  • Lecture 12 Friend Function in C++ Part 1Hindi

Transcription

Use cases

This approach may be used in friendly function when a function needs to access private data in objects from two different classes. This may be accomplished in two similar ways:

  • A function of global or namespace scope may be declared as friend of both classes.
  • A member function of one class may be declared as friend of another one.
// C++ implementation of friend functions.
#include <iostream>
using namespace std;
 
class Foo; // Forward declaration of class Foo in order for example to compile.

class Bar {
  private:
      int a = 0;
  public:
      void show(Bar& x, Foo& y);
      friend void show(Bar& x, Foo& y); // declaration of global friend
};
 
class Foo {
  private:
      int b = 6;
  public: 
      friend void show(Bar& x, Foo& y); // declaration of global friend
      friend void Bar::show(Bar& x, Foo& y); // declaration of friend from other class 
};
 
// Definition of a member function of Bar; this member is a friend of Foo
void Bar::show(Bar& x, Foo& y) {
  cout << "Show via function member of Bar" << endl;
  cout << "Bar::a = " << x.a << endl;
  cout << "Foo::b = " << y.b << endl;
}
 
// Friend for Bar and Foo, definition of global function
void show(Bar& x, Foo& y) {
  cout << "Show via global function" << endl;
  cout << "Bar::a = " << x.a << endl;
  cout << "Foo::b = " << y.b << endl;
}
 
int main() {
   Bar a;
   Foo b;
 
   show(a,b);
   a.show(a,b);
}

References

  1. ^ Holzner, Steven (2001). C++ : Black Book. Scottsdale, Ariz.: Coriolis Group. p. 397. ISBN 1-57610-777-9. When you declare a function a friend of a class, that function has access to the internal data members of that object (that is, its protected, and private data members.)

External links

This page was last edited on 4 November 2023, at 14:48
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.