Created
          July 9, 2015 16:00 
        
      - 
      
- 
        Save krysseltillada/7a2bb924e8f7944332ee to your computer and use it in GitHub Desktop. 
    relationship between *this and function members
  
        
  
    
      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 "Person.h" | |
| #include <iostream> | |
| #include <string> | |
| /// implementation section | |
| Person::Person (std::string n, std::string a) : | |
| name (n), address (a) {} | |
| const Person &Person::display () const | |
| { | |
| std::cout << "const version func is called " << std::endl; | |
| return *this; | |
| } | |
| Person &Person::display () | |
| { | |
| std::cout << "non const version func is called " << std::endl; | |
| return *this; | |
| } | 
  
    
      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
    
  
  
    
  | #ifndef PERSON_H | |
| #define PERSON_H | |
| #include <iostream> | |
| #include <string> | |
| class Person | |
| { | |
| public: | |
| Person () = default; /// here we declared a default constructor | |
| Person (std::string, std::string); /// we declared a second constructor for initialization | |
| const Person &display () const; /// we declare a const function member that returns a reference to const object | |
| Person &display (); /// we declare a non-const function member that returns a reference to object | |
| private: | |
| std::string name, address; /// data members to hold | |
| }; | |
| #endif // PERSON_H | 
  
    
      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> | |
| #include "Person.h" | |
| int main() | |
| { | |
| Person p1; | |
| p1.display().display(); /// this calls the non-const function member of display because the object is non-const which is the *this is default | |
| /// initialize into non-const pointer of *this and we need a member function that have non-const pointer which is a | |
| /// non-const function member of display and returns a reference to object and calls again a display if it doesnt | |
| /// if a return is not a reference to an object a copy of an object will be return not the actual object | |
| const Person p2; | |
| p2.display().display(); /// this calls the const function member of display | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment