简体   繁体   English

我应该从静态成员方法返回什么类型的指针

[英]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.

相关问题 从成员函数指针获取方法的返回类型 - Get the return type of a method from a member function pointer 我应该为指针分配使用哪种返回类型? - What return type should I use for a pointer assignment? 我应该将什么类型的指针传递给C ++ 11中的方法? - What type of pointer should I pass to a method in C++11? 如果我创建一个修改值的迭代器,则静态成员“引用”应该是哪种类型? - What type should static member “reference” be if I create an iterator which modifies the value? 无法从静态方法调用指向成员函数的指针 - Cannot call pointer to member function from static method 这个指针的返回类型是什么? - What is the return type of this pointer? C ++访问器-我应该返回一个指向成员变量或实际成员变量本身的指针吗? - C++ Accessors - should I return a pointer to the member variable or the actual member variable itself? 我应该在简单的访问成员函数中使用const返回类型吗? - Should I use const return type in a simple access member function? 如果不满足条件,我应该在成员函数中返回什么? - What should I return in a member function if a condition is not met? 函数返回一个指向对象的指针,在这种情况下我该返回什么? - function returns a pointer to an object, what should I return in this case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM