I come mostly from the world of Java, but have recently been writing a bunch of c++ and still don't have a great understanding of how to use pointers or what type of pointers to use where. I will give a brief example of my case, and keep in mind the question I am really asking is, "What type of pointer should I use and why?".
A perfect parallel to my code is to consider a class for a chain link. Each link has a parent link, except for the root link of which there can only be 1.
// Link.hpp
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.cpp
Link* Link::rootLink = Link::createRootLink("root");
Link* Link::createRootLink(const std::string &linkName);
{
// Is raw pointer correct here or should I
// use a different pointer type, i.e. smart_pointer,
// shared_pointer, etc?
Link *rootLink = new Link(linkName);
return rootLink;
}
Link* getRootLink()
{
return rootLink;
}
Link::Link(const std::string &linkName)
{
this->linkName = linkName;
this->parentLink = NULL; // Root link has no parent link.
}
Link::Link(const std::string &linkName, Link *parentLink)
{
this->linkName = linkName;
this->parentLink = parentLink;
}
Hopefully this is clear enough. I Java this type of thing is simple, but in c++ I am not sure how to manage the need for pointers to static objects. Thanks in advance for the help!
一个Link*
很好,并且恰好有一个类( static
)没有关系 - 它仍然只是一个指向Link
对象的指针。
I use this (as it is inlinable in the header):
class Link
{
public:
Link(const std::string &linkName, Link *parentLink);
Link(const std::string &linkNam);
static Link* getRootLink() {
static std::unique_ptr<Link> root(new Link("root"));
return root.get();
}
};
In a single threaded environment this is fine, in multithreaded you'll need to protect against multiple threads accessing getRootLink
at the same time for the very first time (there's a guard pattern to enable this.)
Note the unique_ptr
is there to allow the deconstructor to run at the end of your programme. If you know you don't need the deconstructor, a plain old pointer is fine too.
I'd suggest returning a std::unique_pointer<Link>
or std::shared_pointer<Link>
or (before C++11) std::auto_ptr<Link>
.
You can return raw pointers but you will have to manage the pointers (and lifetime of the objects they point to) separately. Since Java does that cleanup for you - and Java developers often encourage you not to worry about such things - you will often not have the mindset to manage such things sensibly.
I don't see why it is returning a pointer at all. I would expect createRootLink()
to return by-value and getRootLink()
to return a reference.
class Link
{
public:
Link(const std::string& name, Link& parent) : name(name), parent(&parent) {}
Link(const std::string& name) : name(name), parent(nullptr) {}
static Link createRootLink(const std::string& name)
{
return Link(name);
}
static Link& getRootLink()
{
static Link rootLink = createRootLink("root");
return rootLink;
}
private:
std::string name;
Link *parent;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.