Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 10, 2015 16:59
Show Gist options
  • Save krysseltillada/8fa76c406a9f4fa04436 to your computer and use it in GitHub Desktop.
Save krysseltillada/8fa76c406a9f4fa04436 to your computer and use it in GitHub Desktop.
declaring function member as friend
#include <iostream>
class B; /// forward declaration to know the types in function members in Class A
class A
{
public:
void modify (B &,const int &);
int &display (B &);
};
class B
{
public:
friend void A::modify (B &,const int &); /// declares function member A::modify a friend
friend int &A::display (B &); /// the same
private:
int data_no = 0;
};
/// declaring this after the declaration of class b to know the compiler what type can be accessed
void A::modify(B &b,const int &i)
{
b.data_no = i;
}
int &A::display (B &b)
{
return b.data_no;
}
int main ()
{
A a;
B b;
a.modify(b, 22);
std::cout << a.display(b) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment