11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

258Part III: Introduction to ClassesThis access allows the function to navigate through the list until the matchingobject is found. The following shows how such static member functions mightbe used:int main(int argcs, char* pArgs[]){Student s1(“Randy”);Student s2(“Jenny”);Student s3(“Kinsey”);Student *pS = Student::findName(“Jenny”);return 0;}What Is This About, Anyway?It’s time to discuss the this keyword, just for grins. this is a pointer to thecurrent object within a member function. It’s used when no other objectname is specified. In a normal member function, this is the implied firstargument to the function, as illustrated here:class SC{public:void nFn(int a); // like SC::nFn(SC *this, int a)static void sFn(int a); // like SC::sFn(int a)};void fn(SC& s){s.nFn(10); // -converts to-> SC::nFn(&s, 10);s.sFn(10); // -converts to-> SC::sFn(10);}That is, the function nFn() is interpreted almost as though it were declaredvoid SC::nFn(SC *this, int a). The call to nFn() is converted by thecompiler as shown, with the address of s passed as the first argument. (Youcan’t actually write the call this way; this is only what the compiler is doing.)References to other non-static members within SC::nFn() automatically usethe this argument as the pointer to the current object. When SC::sFn() wascalled, no object address was passed. Thus, it has no this pointer to usewhen referencing non-static functions, which is why I say that a static memberfunction is not associated with any current object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!