Hello everyone, I'm just returning to C++ after a (very) long break. Ignore the form and purpose of this program, it's just an experiment to get me reacquainted with the language. I put my problem in the comments... thanks in advance.

// Trying to get back into C++.
// It has definitely been a WHILE... so bear
// with me. 
// The question I have is in the global setData function.


#include <iostream>
#include <string>
using namespace std; 

const string endr = "\n\n";

//--------------
//Entry class...
//--------------
class Entry
{
public:
    Entry(string NAME = "<undef>", string AGE = "<undef>", string JOB = "<undef>");
    void set(string NAME, string AGE, string JOB);
    void display();
private:
    string name;
    string age;
    string job;
};


Entry::Entry(string NAME, string AGE, string JOB)
{
    name = NAME;
    age = AGE;
    job = JOB;
}

void Entry::display()
{
    cout << "Name: " << name << endl;
    cout << " Age: " << age << endl;
    cout << " Job: " << job << endr;
}

void Entry::set(string NAME, string AGE, string JOB)
{
    name = NAME;
    age = AGE;
    job = JOB;
}

//------------
//end Entry class
//------------



//------------
//globals...
//------------

void setData(Entry *entry, string NAME, string AGE, string JOB)
{
    entry->set(NAME, AGE, JOB); 

    // ^ Why does that work instead of *entry->set(NAME, AGE, JOB)?
    // I always thought just "entry" would refer to the address rather than
    // the data at the address. This works, but I don't understand why!
}

//------------
//end globals
//------------



int main()
{
    Entry joe;
    setData(&joe, "Joseph Moore", "20", "Freeloader");
    joe.display();

    return 0;
}

Recommended Answers

All 2 Replies

>entry->set(NAME, AGE, JOB);
This is equivalent to:

(*entry).set(NAME, AGE, JOB);

The arrow operator dereferences your pointer for you and then grabs the member you specify.

Excellent. Thanks for the quick reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.