What Does The Word "This" Do In This Program?

Hello! I understand everything in this program except essentially the whole of the member function NameDataSet::add(), specifically the "this" word. I have a guess that it refers to the current object referencing the member function (if this is correct I just needed to confirm I knew what it ment), but I can't make much sense of the rest of the member function. I know it has to do with manipulating the pointers of the class in some way since this program makes a linked list using this class. Can someone interpret NameDataSet::add() for me? Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// LinkedListClass -linked list example with class methods
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

// NameDataSet - stores a person's name (these objects
//               could easily store any other information
//               desired).
class NameDataSet
{
  public:
    NameDataSet(string& refName)
      : sName(refName), pNext(0) {}

    // add self to beginning of list
    void add()
    {
        pNext = pHead;
        pHead = this;
    }

    // access methods
    static NameDataSet* first() { return pHead; }
           NameDataSet* next()  { return pNext; }
          const string& name()  { return sName; }
  protected:
    string sName;

    // the link to the first and next member of list
    static NameDataSet* pHead;
    NameDataSet* pNext;
};

// allocate space for the head pointer
NameDataSet* NameDataSet::pHead = 0;

// getData - read a name and social security
//           number; return null if no more to
//           read
NameDataSet* getData()
{
    // read the first name
    string name;
    cout << "Enter name:";
    cin  >> name;

    // if the name entered is 'exit'...
    if (name == "exit")
    {
        // ...return a null to terminate input
        return 0;
    }

    // otherwise, return an object with that name
    return new NameDataSet(name);
}

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "Read names of students\n"
         << "Enter 'exit' for first name to exit"
         << endl;

    // create (another) NameDataSet object
    NameDataSet* pNDS;
    while (pNDS = getData())
    {
        // add it to the list of NameDataSet objects
        pNDS->add();
    }

    // to display the objects, iterate through the
    // list (stop when the next address is NULL)
    cout << "\nEntries:" << endl;
    for(NameDataSet *pIter = NameDataSet::first();
        pIter;
        pIter = pIter->next())
    {
        // display name of current entry
        cout << pIter->name() << endl;
   }

    // wait until user is ready before terminating program
    // to allow the user to see the program results
    system("PAUSE");
    return 0;
}
 
the comment on line 16 is telling you really.

this is a pointer to the current object. If you instantiate an object of class NameDataSet and call Add() it's saying "make me the head of the list".
http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/
Last edited on
Topic archived. No new replies allowed.