相关疑难解决方法(0)

为什么 make_unique 使用私有构造函数失败?

我正在实现一个单例类作为 c++14 中的练习,其中必须使用智能指针。此代码片段有效,但我想使用 make_unique ,不要与旧的 new 和 delete 关键字混淆,但它说它无法访问私有成员。为什么会发生?关键字“new”与智能指针一起使用是否安全?

#include <iostream>
#include <memory>

using namespace std;

class foo
{
    private:
        int val_;

    private:    
        foo() {}

    public:
        // properties
        void set(const int v) { val_ = v; }
        int  get() const { return val_; }

        //methods
        static const unique_ptr<foo>& get_instance()
        {
            static unique_ptr<foo> pfoo( new foo() );
            //unique_ptr<foo> pfoo = make_unique<foo>() ;  // Error - cannot access private member declared in class 'foo'
            return pfoo;        
        }


};

int main()
{
    foo::get_instance()->set(100); …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

5
推荐指数
0
解决办法
5200
查看次数

仅当共享指针时才阻止创建类

如何防止人们创建类的实例,而只创建共享指针?我正在做类似的事情:

class OnlyShred
{
public:
    friend std::shared_ptr<OnlyShred> make_shared()
    {
        return std::make_shared<OnlyShred>();  
    };

private:
    OnlyShred() = default;
    OnlyShred(const OnlyShred&) = delete;
    OnlyShred(const OnlyShred&&) = delete;
    OnlyShred& operator=(const OnlyShred&) = delete;
};
Run Code Online (Sandbox Code Playgroud)

您能确认一下这是否可以吗?如果你能想到这样做有什么缺点吗?该实例无法复制/移动,因此这必须保证当有人使用此类时只有共享指针存在?

c++ copy instance shared-ptr

4
推荐指数
1
解决办法
396
查看次数

使用unique_ptr的单例对象

我有以下CPP源代码,用于使用unique_ptr创建类的Singleton对象:

#include <iostream>
#include <memory>

class A
{

public:
    std::unique_ptr<A> getInstance(int log);
    ~A();

private:
    static bool instanceFlag;
    static std::unique_ptr<A> single;
    A(int log);
    int mLog;
};

bool A::instanceFlag = false;
std::unique_ptr<A> A::single = NULL;

std::unique_ptr<A> A::getInstance(int log)
{
    if(!instanceFlag)
    {
        //single = std::make_unique<A>(log);
        single = std::unique_ptr<A>(new A(log));
        instanceFlag = true;
        return std::move(single);
    }
    else
    {
        return std::move(single);
    }
}

A::A(int log) :
    mLog(log)
{

    std::cout << "Called A cons" << std::flush << std::endl;
}

int main()
{
std::unique_ptr<A> mA = A::getInstance(5); …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

2
推荐指数
2
解决办法
4346
查看次数

如何通过传递“ this”关键字来分配weak_ptr?

在我的程序中,组将共享指向主题的指针;主题对他们的组的指示力很弱。我希望该组具有一个join()函数,该函数将Subject的弱指针分配给它自己。以下是我尝试过的最小代码。如何修复join()函数?

#include <iostream>
#include <string>
#include <memory>

class Party;

class Subject
{
public:
    std::weak_ptr<Party> MyParty;
};

class Party
{
public:
    std::string Name;

    void join(std::shared_ptr<Subject> subject)
    {
        subject->MyParty = std::make_shared<Party>(*this); // <---- PROBLEM
    }
};

int main()
{
    auto& BlueParty = std::make_shared<Party>();
    BlueParty->Name = "Blue Party";

    auto& Jane = std::make_shared<Subject>();

    BlueParty->join(Jane);

    if (auto ptr = Jane->MyParty.lock())
    { 
        std::cout << "I am in " << ptr->Name << std::endl; 
    }

    else { std::cout << "I have no party." << std::endl; }

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ this weak-ptr

1
推荐指数
1
解决办法
81
查看次数

如何避免使用shared_ptr悬空指针?

我有一个类,其对象指针将作为键/数据添加到多个 std::map/std::unordered_map/hash(内部实现)中。为了自动删除对象,我使用了 shared_ptr。

我使用shared_ptr only类设计了我的类。

现在我想确保将来没有人这样做:

#include <memory>
#include <string>

class A {
 protected:
   struct this_is_private;

 public:
   explicit A(const this_is_private &) {}
   A(const this_is_private &, ::std::string, int) {}

   template <typename... T>
   static ::std::shared_ptr<A> create(T &&...args) {
      return ::std::make_shared<A>(this_is_private{0},
                                   ::std::forward<T>(args)...);
   }

 protected:
   struct this_is_private {
       explicit this_is_private(int) {}
   };

   A(const A &) = delete;
   const A &operator =(const A &) = delete;
};


::std::map<A*, int> m_error;
::std::map<::std::shared_ptr<A>, int> m_ok;

::std::shared_ptr<A> foo()
{
   ::std::shared_ptr<A> temp = A::create();

   A * obj_ptr …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management stl smart-pointers c++11

1
推荐指数
1
解决办法
1026
查看次数