[英]What type of pointer should I return from static member method
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. 我主要来自Java世界,但最近一直在编写一堆c ++,但仍然没有很好地理解如何使用指针或使用什么类型的指针。 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.
每个链接都有一个父链接,除了根链接只能有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.
我在Java这类事情很简单,但在c ++中我不知道如何管理对静态对象指针的需求。 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.) 在单线程环境中这很好,在多线程中你需要防止多个线程同时第一次访问
getRootLink
(有一个保护模式来启用它。)
Note the unique_ptr
is there to allow the deconstructor to run at the end of your programme. 请注意,
unique_ptr
允许解构器在程序结束时运行。 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>
. 我建议返回一个
std::unique_pointer<Link>
或std::shared_pointer<Link>
或(在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.
由于Java为您做了清理 - 而Java开发人员经常鼓励您不要担心这些事情 - 您通常不会有理智地管理这些事情。
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. 我希望
createRootLink()
返回by-value, getRootLink()
返回引用。
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;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.