<<< C++ references     Index     Returning *this >>>

19. The this keyword


  • Within a member function, the this keyword is a pointer to the current object.

  • Current object is the object through which the function was called.

  • Majority of member functions never use the this pointer, because its use within the function is implicit:

  • 
    struct Rectangle {
        int width;  // member variable
        int height; // member variable
        char const* ptr_name;
        //...
        void set_name( char const* ptr_name_ )
        {
            // the use of the this keyword
            // is redundant and unnecessary:
            this->ptr_name = ptr_name_;
        }
        char const* get_name() const
        {
            // the use of the this keyword
            // is redundant and unnecessary:
            return this->ptr_name;
        }
        //...
    };//struct Rectangle
    
    

<<< C++ references     Index     Returning *this >>>