11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 19: Static Members: Can Fabric Softener Help? 257class Student{public:// the following is not legalstatic char* sName(){return pName; // which pName? there’s no object}// ...other stuff the same...protected:char* pName;static int noOfStudents;};That’s not to say that static member functions have no access to non-staticdata members. Consider the following useful function findName() that findsa specific object in a linked list (see Chapter 14 for an explanation of howlinked lists work). The majority of the code necessary to make the linked listwork is left as an exercise for the reader. Don’t you just hate that phrase? But,seriously, the linked list code is already in Chapter 14:class Student{public:Student(char *pName){// ...construct the object and add it to a// list of Student objects...}// findName - return student w/specified namestatic Student *findName(char *pName){// ...starting from the first object in the list// which is pointed at by pHead link through// the list using pNext until the correct// object is found...}protected:static Student *pHead;Student *pNext;char* pName;};Student* Student::pHead = 0;The function findName() has access to pHead because all objects share it.Being a member of class Student, findName() also has access to pNext.

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

Saved successfully!

Ooh no, something went wrong!