DOC PREVIEW
FSU COP 3330 - This Pointer

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The This Pointer Programming in C++ Fall 2008The This PointerSlide 3The This PoinerSlide 5The This PointerProgramming in C++Fall 2008 Dr. David A. [email protected] This Pointer•The Problem: “Yes, inside of a class you have access to all of the private data, but how do you access the object itself like a client would?” •Each object maintains a pointer to itself which is called the “this” pointer. •Each object can determine it’s own address by using the “this” keyword. •Many member functions of a class in C++ require no arguments because of the use of the implicit pointer “this”. Very handy to use this which avoids the overhead of passing parameters but still enforces the rules of good sound software engineering by using the appropriate class functions.The This Pointer// Example of using the this pointer#include <iostream.h>using namespace std;class Test{public: Test (int=0); void print () const;private:int x;};The This PoinerTest::Test (int a ) // Constructor{ x = a;}void Test::print() const{ cout << " x is equal to " << x ; cout << "\n this-> is equal to "<<this->x; cout << "\n (*this).x is equal to "<< (*this).x <<endl;}int main (void){ Test testobject(12); testobject.print(); return 0;}The This Pointer•It may seem redundant but the “this” pointer does have some uses:–Prevents an object from being assigned to itself. –Enables cascading member function


View Full Document

FSU COP 3330 - This Pointer

Download This Pointer
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view This Pointer and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view This Pointer 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?