class Link
{
private:
string value;
Link* succ;
Link* prev;
public:
Link(string v, Link* s = nullptr, Link* p = nullptr)
: value(v), succ(s), prev(p) { }
Link* insert(Link* p)
{
if(p == nullptr) return this;
p->succ = this;
if(this->prev) this->prev->succ = p->prev;
p->prev = this->prev;
this->prev = p;
return p;
}
};
int main()
{
Link* language = new Link{Link("C++", nullptr, nullptr)};
language = language->insert(new Link("Python", language, nullptr));
return 0;
}
In the book, programming principles and practice, there is a chapter where you implement a part of a list and here's the code.
In the above code, insert() takes Link* as an argument and uses -> to change what it's pointing at(Link* succ, prev) directly. But both succ and prev are private members of the class so I don't understand how it's possible for insert() to access them directly.