Created
          July 10, 2015 16:59 
        
      - 
      
- 
        Save krysseltillada/8fa76c406a9f4fa04436 to your computer and use it in GitHub Desktop. 
    declaring function member as friend
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #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