简体   繁体   English

尝试制作shared_ptr时std :: make_shared()中出现错误?

[英]Errors in std::make_shared() when trying to make shared_ptr?

(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). (使用Visual Studio 2010)我正在尝试在项目中创建一个现有类的shared_ptr(该类是在std :: shared_ptr存在十年之前编写的)。 This class takes a non-const pointer to another object, it's empty parameter constructor is private. 此类采用非常量指针指向另一个对象,它的空参数构造函数是私有的。

class Foobar {
public:
    Foobar(Baz* rBaz);

private:
    Foobar();
}

When I try to create a shared_ptr to it, things don't go well: 当我尝试为其创建一个shared_ptr时,事情进展不顺利:

Baz* myBaz = new Baz();
std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz));

On VS2010, this gives me 在VS2010上,这给了我

error C2664: 'Foobar::Foobar(const Foobar &)' : cannot convert parameter 1 from 'Foobar *' to 'const Foobar &'
3>          Reason: cannot convert from 'Foobar *' to 'const Foobar'

For some reason it appears to be calling the copy constructor of Foobar instead of the constructor that takes a Baz* . 由于某种原因,它似乎正在调用Foobar的副本构造函数,而不是采用Baz*的构造函数。

I'm also not sure about the cannot convert from 'Foobar *' to 'const Foobar' part. 我也不确定cannot convert from 'Foobar *' to 'const Foobar'部分。 My best interpretation is that my templated-type of shared_ptr<Foobar> is wrong. 我最好的解释是我的shared_ptr<Foobar>模板类型是错误的。 I made it shared_ptr<Foobar*> but this seems wrong, all examples I've seen don't make the type a raw pointer. 我将其设置为shared_ptr<Foobar*>但这似乎是错误的,我看到的所有示例都没有将类型设为原始指针。

It seems that making everything shared_ptr<Foobar*> compiles properly, but will that prevent the Foobar object from getting deleted properly when all shared_ptr 's go out of scope? 似乎使所有shared_ptr<Foobar*>都能正确编译,但是当所有shared_ptr超出范围时,这是否会阻止Foobar对象被正确删除?

Edit: This seems related, but I'm not using Boost: boost make_shared takes in a const reference. 编辑:这似乎有关,但我没有使用Boost: boost make_shared接受const引用。 Any way to get around this? 有什么办法解决这个问题?

Edit2: For clarity, if you're wondering why I'm using make_shared() , in my actual code the sharedFoo variable is a class member of a third class (independent of Foobar and Baz ). Edit2:为清楚起见,如果您想知道为什么我使用make_shared() ,那么在我的实际代码中, sharedFoo变量是第三类的类成员(独立于FoobarBaz )。

That should be; 那应该是;

std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(myBaz);

...since make_shared constructs the actual object for you by running the constructor. ...因为make_shared通过运行构造函数为您构造了实际对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 make_shared创建std :: shared_ptr吗? gcc 4.6.2 - make_shared create std::shared_ptr? gcc 4.6.2 Qt :: make_shared用于创建QSharedPtr作为std :: make_shared用于创建std :: shared_ptr - Qt::make_shared for creating QSharedPtr as std::make_shared for creating std::shared_ptr std::shared_ptr 与 std::make_shared:意外缓存未命中和分支预测 - std::shared_ptr vs std::make_shared: unexpected cache misses and branch prediction 使用make_shared <std :: thread>创建shared_ptr <std :: thread>的实例 - Creating an instance of shared_ptr<std::thread> with make_shared<std::thread> 从std :: make_shared到std :: shared_ptr的分配静默失败? (VS2012错误) - Assignment from std::make_shared to std::shared_ptr fails silently? (VS2012 Bug) 的std :: shared_ptr的 <type> (new DerivedType(…))!= std :: make_shared <type> (DerivedType(...))? - std::shared_ptr<type>(new DerivedType(…)) != std::make_shared<type>(DerivedType(…))? 对创建的shared_ptr的boost :: make_shared引用 - boost::make_shared reference to created shared_ptr 使用make_shared通过shared_ptr包装动态数组 - Wrap dynamic array with shared_ptr by make_shared 使用make_shared创建shared_ptr是否有任何缺点 - Are there any downsides with using make_shared to create a shared_ptr shared_ptr <>到数组自定义删除器(使用make_shared) - shared_ptr<> to an array custom deleter (with make_shared)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM