I come mainly from the Java world, but I recently wrote a bunch of C ++ and still do not really understand how to use pointers or what types of pointers to use there. I will give a brief example of my case and keep in mind the question that I really ask: "What type of pointer should I use and why?".
The perfect parallel with my code is to consider a class for a chain link. Each link has a parent link, with the exception of the root link which can be only 1.
class Link
{
public:
Link(const std::string &linkName, Link *parentLink);
Link(const std::string &linkNam);
static Link* createRootLink(const std::string &linkName);
static Link* getRootLink();
private:
static Link *rootLink;
Link *parentLink;
}
Link* Link::rootLink = Link::createRootLink("root");
Link* Link::createRootLink(const std::string &linkName);
{
Link *rootLink = new Link(linkName);
return rootLink;
}
Link* getRootLink()
{
return rootLink;
}
Link::Link(const std::string &linkName)
{
this->linkName = linkName;
this->parentLink = NULL;
}
Link::Link(const std::string &linkName, Link *parentLink)
{
this->linkName = linkName;
this->parentLink = parentLink;
}
Hope this is clear enough. I Java this type of thing is simple, but in C ++ I'm not sure how to control the need for pointers to static objects. Thank you in advance for your cooperation!